Laravel 4.2 多对多关系,使用其他值然后使用默认 ID
Laravel 4.2 many to many relation, use something else then the default ID
我有 2 个模型,它们都没有使用 table 中的 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_products.internal_id和seoshop_categories.internal_id两个字段都存在,列类型都是int(11)。
谁能告诉我出了什么问题?
tables 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();
});
}
好的,现在我已经创建了我的 table,并且它按应有的方式工作。我需要让我的产品有类别(多 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');
}
当您在模型中定义关系时,您可以告诉 Laravel 本地键和外键是什么...
class Product extends Eloquent
{
public function categories() {
return $this->hasMany('Category', 'internal_id', 'id');
}
}
class Category extends Eloquent
{
public function products() {
return $this->hasMany('Product', 'internal_id', 'id');
}
}
要设置外键约束,字段定义需要完全匹配。然而,在这种情况下,seoshop_category_product.category_id
字段被定义为 UNSIGNED INT,而引用的 seoshop_categories.internal_id
字段被定义为 SIGNED INT。您产品的外键也是如此。
因此,您可以将类别和产品 table 上的 internal_id
字段更新为未签名,或者您可以将数据透视 table 上的外键字段更新为被签名。
我有 2 个模型,它们都没有使用 table 中的 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_products.internal_id和seoshop_categories.internal_id两个字段都存在,列类型都是int(11)。
谁能告诉我出了什么问题?
tables 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();
});
}
好的,现在我已经创建了我的 table,并且它按应有的方式工作。我需要让我的产品有类别(多 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');
}
当您在模型中定义关系时,您可以告诉 Laravel 本地键和外键是什么...
class Product extends Eloquent
{
public function categories() {
return $this->hasMany('Category', 'internal_id', 'id');
}
}
class Category extends Eloquent
{
public function products() {
return $this->hasMany('Product', 'internal_id', 'id');
}
}
要设置外键约束,字段定义需要完全匹配。然而,在这种情况下,seoshop_category_product.category_id
字段被定义为 UNSIGNED INT,而引用的 seoshop_categories.internal_id
字段被定义为 SIGNED INT。您产品的外键也是如此。
因此,您可以将类别和产品 table 上的 internal_id
字段更新为未签名,或者您可以将数据透视 table 上的外键字段更新为被签名。