php - Foreign Key issue with Laravel migration -
i've been looking around on here answer nothing seems work. i've done base install of laravel , sentry 2. firstly i've added migration add client_id column users table.
schema::table('users', function(blueprint $table) { $table->integer('client_id')->unique; });
then next created clients table following in different migration.
schema::create('clients', function(blueprint $table) { $table->increments('id'); $table->timestamps(); $table->integer('client_id')->unique; $table->string('client_name'); $table->date('expiry'); $table->integer('cals'); });
finally created migration following in.
schema::table('users', function(blueprint $table) { $table->foreign('client_id') ->references('client_id')->on('clients') ->ondelete('cascade'); });
the error im getting following;
sqlstate[hy000]: general error: 1215 cannot add foreign key constraint (sql: alter table `users` add constraint use rs_client_id_foreign foreign key (`client_id`) references `clients` (`client_id`) on delete cascade)
i'm little stuck on should looking for, should both indexes?
$table->integer('client_id')->unique;
this line should read:
$table->integer('client_id')->unique();
regardless, if you're trying reference 'id' column on 'clients' table foreign key constraint on 'users' table, following:
schema::table('users', function(blueprint $table) { $table->integer('client_id')->unsigned(); $table->foreign('client_id') ->references('id')->on('clients') ->ondelete('cascade'); }); schema::create('clients', function(blueprint $table) { $table->increments('id'); $table->timestamps(); $table->string('client_name'); $table->date('expiry'); $table->integer('cals'); });
Comments
Post a Comment