php – Laravel 4.2多对多关系,使用其他东西然后默认ID
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Laravel 4.2多对多关系,使用其他东西然后默认ID,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2998字,纯文字阅读大概需要5分钟。
内容图文
![php – Laravel 4.2多对多关系,使用其他东西然后默认ID](/upload/InfoBanner/zyjiaocheng/773/93857d1ba3cc4b45ae3e43fa44441609.jpg)
我有两个模型,他们都没有使用表中的ID,但字段internal_id.所以我定制了我的枢轴架构,但我一直坚持连接它们.我得到错误:
General error: 1215 Cannot add foreign key constraint (SQL: alter table `seoshop_category_product` add constraint seoshop_category_product_category_id_foreign foreign key
(`category_id`) references `seoshop_categories` (`internal_id`) on delete cascade)
迁移的代码是:
Schema::create('seoshop_category_product', function(Blueprint $table)
{
$table->increments('id');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('internal_id')->on('seoshop_categories')->onDelete('cascade');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('internal_id')->on('seoshop_products')->onDelete('cascade');
$table->timestamps();
});
作为seoshop_categories.internal_id的seoshop_products.internal_id这两个字段都是现有的,列类型都是int(11).
谁能告诉我出了什么问题?
表seoshop_categories和seoshop_products的迁移
//seoshop_products
public function up()
{
Schema::create('seoshop_products', function(Blueprint $table)
{
$table->increments('id');
$table->integer('shop_id');
$table->integer('internal_id')->signed()->index();
$table->integer('internal_variant_id');
$table->string('visible');
$table->string('tags');
$table->timestamps();
});
}
//Table seoshop_categories
public function up()
{
Schema::create('seoshop_categories', function(Blueprint $table)
{
$table->increments('id');
$table->integer('internal_id')->signed()->index();
$table->datetime('seoshop_created_at');
$table->datetime('seoshop_updated_at');
$table->text('full_description');
$table->timestamps();
});
}
好的,所以现在我已经创建了我的表,它的工作原理应该如何.我需要让我的产品有类别(多2个).所以我用
SEOshopProduct::find(1)->with('categories')->get();
在dd()之后,类别为空,我查看了我的查询如何调用它:
[8] array(3) {
["query"] "select `seoshop_categories`.*, `seoshop_category_product`.`product_id` as `pivot_product_id`, `seoshop_category_product`.`category_id` as `pivot_category_id` from `seoshop_categories` inner join `seoshop_category_product` on `seoshop_categories`.`id` = `seoshop_category_product`.`category_id` where `seoshop_category_product`.`product_id` in (?)"
["bindings"] array(1) {
[0] 8
}
["time"] 0.37
}
产品和类别的internal_id大于10.000,我在查询中看不到它.
我的模特:
产品:
public function categories(){
return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'product_id', 'category_id');
}
分类:
public function products(){
return $this->belongsToMany('SEOshopCategory', 'seoshop_category_product', 'category_id', 'product_id');
}
解决方法:
要设置外键约束,字段定义需要完全匹配.但是,在这种情况下,seoshop_category_product.category_id字段被定义为UNSIGNED INT,但引用的seoshop_categories.internal_id字段被定义为SIGNED INT.对于您的产品的外键也是如此.
因此,您可以将类别和产品表上的internal_id字段更新为未签名,也可以更新数据透视表上的外键字段以进行签名.
内容总结
以上是互联网集市为您收集整理的php – Laravel 4.2多对多关系,使用其他东西然后默认ID全部内容,希望文章能够帮你解决php – Laravel 4.2多对多关系,使用其他东西然后默认ID所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。