一般错误:1215无法添加外键约束

时间:2016-03-01 10:23:31

标签: php mysql laravel

我正在尝试从price表中获取products值并将其设置为sold表。你可以告诉我为什么我收到这个错误? product_iduser_id工作正常。但是当我需要在price

中创建外来sold时,我会收到错误

SOLD

    Schema::create('sold', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');    
        $table->integer('product_id')->unsigned();  
        $table->foreign('product_id')->references('id')->on('products');
        $table->integer('price')->unsigned();
        $table->foreign('price')->references('price')->on('products');   
        $table->integer('bid_price')->unsigned();
        $table->foreign('bid_price')->references('bid_price')->on('products');     
        $table->timestamps();
    });

PRODUCTS

    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('slug');
        $table->string('title');
        $table->text('body');
        $table->integer('price');
        $table->integer('bid_price');
        $table->string('address');
        $table->string('condition');
        $table->integer('quantity');
        $table->string('img_1');
        $table->string('img_2');
        $table->string('img_3');
        $table->string('img_4');
        $table->integer('views');
        $table->timestamps();
    });

错误讯息:

General error: 1215 Cannot add foreign key constraint (SQL
  : alter table `sold` add constraint sold_price_foreign foreign key (`price`
  ) references `products` (`price`))

修改

`LATEST FOREIGN KEY ERROR
------------------------
2016-03-01 12:40:08 31a0 Error in foreign key constraint of table auction/#sql-2564_ef:
foreign key (`price`) references `products` (`price`):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.`

2 个答案:

答案 0 :(得分:0)

点击此链接:MySQL Cannot Add Foreign Key Constraint

第二个答案是:

  

我设置了一个字段为"无符号"而另一个则没有。一旦我将两列都设置为无符号就可以了。

所以我会假设制作专栏"价格"对你的产品也没有签名,应该解决问题。

像这样:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->string('slug');
    $table->string('title');
    $table->text('body');
    $table->integer('price')->unsigned(); // <-- the difference is here!
    $table->integer('bid_price');
    $table->string('address');
    $table->string('condition');
    $table->integer('quantity');
    $table->string('img_1');
    $table->string('img_2');
    $table->string('img_3');
    $table->string('img_4');
    $table->integer('views');
    $table->timestamps();
});

<强>更新

&#34;最后的外键错误&#34;部分告诉你:

  

在引用的表

中找不到索引

MySQL documentation说的是外键:

  

MySQL需要外键索引和引用键

所以基本上你必须在products表的price列中添加一个索引。

我从未与Laravel合作,但根据他们的文档,它应该像这样工作:

$table->index('price');

更新2 : 我这里只考虑了技术方面,但是以语义方式查看数据库结构,在价格列上创建外键可能是一个坏主意,因为价格实际上不是产品的标识符(具有相同的产品)价格确实存在!)。

答案 1 :(得分:0)

  

在引用的表

中找不到索引

您的产品表需要在您尝试引用的列上使用INDEX。我要说这必须是唯一的,否则MySQL无法确定它引用哪一行。