mercredi 12 juillet 2017

Is it possible to create a foreign key relationship between two tables on String/varchar type columns in Laravel?

I have a categories table which has an id and a slug(varchar type) column.I want to create a subcategories table which will have a column category_slug(varchar type) that will reference the slug column in the Category table.

I am getting errors during php artisan migrate.

    Schema::create('subcategories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('category_slug')->unique();
        $table->foreign('category_slug')
       ->references('slug')->on('categories');
        $table->timestamps();
  });

The errors in the terminal are.

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table laravel_eshopper. #sql-ee8_272 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table tabpanes add constraint tabpanes_category_slug_foreign foreign key (category_slug) references categories (slug))

[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table laravel_eshopper. #sql-ee8_272 (errno: 150 "Foreign key constraint is incorrectly formed")



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2u7WrPo
via IFTTT

Aucun commentaire:

Enregistrer un commentaire