gpt4 book ai didi

database - laravel 4 中的外键跨数据库错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:50 24 4
gpt4 key购买 nike

我使用外键“user_id”迁移到另一个数据库中的表“users”:当我键入 (php artisan migrate) 时,这会产生错误。

[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)

现在 laravel 创建了除 'user_id' 列之外的表。为什么 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.

最佳答案

在声明外键之前,必须先声明字段。

你需要先声明

Schema::create('safetyreports', function(Blueprint $table) { 
...
$table->integer('user_id')->unsigned();
...
});

注意:unsigned 部分对此不可预测。

一旦声明了字段,您就可以像以前一样自由地声明它的关系。

希望对你有帮助... =D

关于database - laravel 4 中的外键跨数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com