Migration: Cannot add foreign key constraint in laravel -


i'm trying create foreign keys in laravel when migrate table using artisan thrown following error:

[illuminate\database\queryexception] sqlstate[hy000]: general error: 1215 cannot add foreign key constraint (sql : alter table `priorities` add constraint priorities_user_id_foreign foreign  key (`user_id`) references `users` (`id`))      

my migration code so:

priorities migration file

public function up() {     //     schema::create('priorities', function($table) {         $table->increments('id', true);         $table->integer('user_id');         $table->foreign('user_id')->references('id')->on('users');         $table->string('priority_name');         $table->smallinteger('rank');         $table->text('class');         $table->timestamps('timecreated');     }); }  /**  * reverse migrations.  *  * @return void  */ public function down() {     //     schema::drop('priorities'); } 

users migration file

public function up() {     //     schema::table('users', function($table)     {     $table->create();     $table->increments('id');     $table->string('email');     $table->string('first_name');     $table->string('password');     $table->string('email_code');     $table->string('time_created');     $table->string('ip');     $table->string('confirmed');     $table->string('user_role');     $table->string('salt');     $table->string('last_login');      $table->timestamps();     }); }  /**  * reverse migrations.  *  * @return void  */ public function down() {     //         schemea::drop('users'); } 

any ideas i've done wrong, want right now, i've got lot of tables need create e.g. users, clients, projects, tasks, statuses, priorities, types, teams. ideally want create tables hold data foreign keys, i..e clients_project , project_tasks etc.

hope can me started.

thanks in advance.

add in 2 steps, , it's make unsigned too:

public function up() {     schema::create('priorities', function($table) {         $table->increments('id', true);         $table->integer('user_id')->unsigned();         $table->string('priority_name');         $table->smallinteger('rank');         $table->text('class');         $table->timestamps('timecreated');     });     schema::table('priorities', function($table) {        $table->foreign('user_id')->references('id')->on('users');    });  } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -