php - Can't add Constraint -
i kept getting exception:
[illuminate\database\queryexception] sqlstate[hy000]: general error: 1215 cannot add foreign key constraint (sql : alter table `messages` add constraint messages_from_foreign foreign key ( `from`) references `id` (`users`)) [pdoexception] sqlstate[hy000]: general error: 1215 cannot add foreign key constraint
here's createmessagestable
class:
public function up() { schema::create('messages', function(blueprint $table) { $table->bigincrements('id'); $table->biginteger('from', false, true); ... $table->foreign('from')->references('users')->on('id'); }); }
and here's createuserstable
class:
public function up() { // http://laravel.com/docs/5.0/schema schema::create('users', function(blueprint $table) { $table->biginteger('id', true, true); ... }); }
i tried double check if had same data type column id
in users
, from
in messages
, , were. wonder wrong , why keep getting constraint error messages.
here's problem:
$table->foreign('from')->references('users')->on('id');
the values should other way around - on
uses table name , references
points column on table.
source: laravel docs