我使用外键“user_id”迁移到另一个数据库中的一个表“users”:这会在我输入(php迁移)时生成一个错误。
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'user_id
' doesn't exist in table (SQL: alter table `safetyreports` add constraint s
afetyreports_user_id_foreign foreign key (`user_id`) references `users` (`i
d`) on delete cascade)现在,除了'user_id‘栏之外,laravel制作了桌子。为什么laravel没有做纵队?
这是我的迁移代码:
Schema::create('safetyreports', function(Blueprint $table) {
$table->increments('id');
$table->string('afdeling');
$table->string('contractor');
$table->string('directe chef');
$table->string('ploeg');
$table->string('team');
$table->string('plant_afd');
$table->string('datum');
$table->string('plaats');
$table->string('tijd');
$table->string('omschrijving');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->timestamps();
});
This is a the important part of my Users model code:
protected $connection = 'mysql2';
protected $table = 'users';
I have tested my Users model. It works. I perform (cross-database) user authentication with this model. 发布于 2014-02-27 12:37:53
在声明外键之前,必须先声明字段。
你需要先声明
Schema::create('safetyreports', function(Blueprint $table) {
...
$table->integer('user_id')->unsigned();
...
});关注点:对于这一点,unsigned部分是不可预测的。
一旦您声明了字段,您就可以像您所做的那样声明它的关系。
希望能帮上忙..。=D
发布于 2015-07-03 16:26:22
之所以会出现这种情况,是因为您试图为表中尚未存在的列创建外键。看看--假装输出。在创建地址和点表之前创建外键。移动,创建另一个迁移的钥匙,它将工作湖中的一个魅力。
https://stackoverflow.com/questions/22068271
复制相似问题