首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel 4中的外键交叉数据库错误

laravel 4中的外键交叉数据库错误
EN

Stack Overflow用户
提问于 2014-02-27 12:16:20
回答 2查看 1.8K关注 0票数 2

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

代码语言:javascript
复制
[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没有做纵队?

这是我的迁移代码:

代码语言:javascript
复制
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. 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-27 12:37:53

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

你需要先声明

代码语言:javascript
复制
Schema::create('safetyreports', function(Blueprint $table) { 
        ...
        $table->integer('user_id')->unsigned();
        ...
});

关注点:对于这一点,unsigned部分是不可预测的。

一旦您声明了字段,您就可以像您所做的那样声明它的关系。

希望能帮上忙..。=D

票数 4
EN

Stack Overflow用户

发布于 2015-07-03 16:26:22

之所以会出现这种情况,是因为您试图为表中尚未存在的列创建外键。看看--假装输出。在创建地址和点表之前创建外键。移动,创建另一个迁移的钥匙,它将工作湖中的一个魅力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22068271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档