我知道很多人多次问过同样的问题,但似乎我无法让相同的解决方案对我有用。
我无法为up()
下面给出的相关表设置外键:
//Migrate 1 Starts
public function up()
{
Schema::create("UserTable", function($table){
$table->tinyInteger("ApplicationID");
$table->bigInteger("UserID");
$table->string("UserName")->unique();
$table->timestamps();
});
}
//Migrate 1 Ends
//Migrate 2 Starts
public function up()
{
Schema::create("Membership", function($table){
$table->tinyInteger("ApplicationID");
$table->bigInteger("UserID");
$table->string("Password");
}
}
//Migrate 2 Ends
//Migrate 3 Starts: This has the latest timestamp, so it runs after all tables are created
public function up()
{
Schema::table('UserTable', function($table) {
$table->primary("UserID");
$table->foreign("UserID")->references("UserID")->on("Membership");
});
Schema::table('Membership', function($table) {
$table->primary("UserID");
$table->foreign("UserID")->references("UserID")->on("UserTable");
});
}
//Migrate 3 Ends
我得到的错误是SQLSTATE[HY000]: General rror: 1215 Cannot add foreign key constraint (SQL: alter table 'Membership' add constraint membership_userid_foreign foreign key ('UserID') references 'UserTable' ('UserID'))