首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQLSTATE[42S22]:未找到列:“字段列表”中的1054个未知列“id”

SQLSTATE[42S22]:未找到列:“字段列表”中的1054个未知列“id”
EN

Stack Overflow用户
提问于 2020-08-10 05:06:50
回答 1查看 708关注 0票数 0

我有product_variants表,它与另外两个表--数据、productsvariants --有关

当我试图更新我的产品时,它会返回这个错误。

“字段列表”(SQL: update product_variants set id = 0118d682-970f-4859-b30b-e1c79df9c342,parent_id = ?,name = Interface,slug =接口,photo = ?,type = textarea,active = yes,created_at = 2020-08-05 14:57:42,updated_at = 2020-08-05 14:57:42,d14=2020-08-05 14:57:42,d14="variant_id":"0118d682-970f-4859-b30b-e1c79df9c342"},children = {"id":"7efe338d-9026-4fe8-b16f-6b99bc51871a","parent_id":"0118d682-970f-4859-b30b-e1c79df9c342","name":"1x RJ45,1x SD (XC/HC)读卡器,1x (4K @60 HC) HDMI,1x Mini,2x Type-A USB3.2 Gen1,1x Type-A USB3.2 Gen2,1x Type-C (USB3.2 Gen2 / DP),1x类型-C USB3.2 Gen2x2",“段塞”:“1x RJ45,1x SD (XC/HC)读卡器,1x (4K @60 HC) HDMI,1x微型显示端口,2x类型-A USB3.2 Gen1,1x类型-A USB3.2 Gen2,1x Type-C (USB3.2 Gen2 / DP),1x类型-C USB3.2 Gen2x2",”照片“:空”类型“:”文本区域“,”活动“:”是“,"created_at":"2020-08-05 14:57:42","updated_at":"2020-08-05 14:57:42"}其中product_id = 4ea5f41f-aa2f-46d4-b033-e4e875cc8ff4,variant_id (0))

代码

product_variants迁移

代码语言:javascript
复制
public function up()
{
    Schema::create('product_variants', function (Blueprint $table) {
        $table->primary(['product_id', 'variant_id']);
        $table->uuid('product_id')->nullable();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->uuid('variant_id')->nullable();
        $table->foreign('variant_id')->references('id')->on('variants');
    });
}

product model

代码语言:javascript
复制
public function variations(){
    return $this->belongsToMany(Variant::class, 'product_variants', 'product_id', 'variant_id');
}

variant model

代码语言:javascript
复制
public function products(){
    return $this->belongsToMany(Product::class, 'product_variants', 'variant_id', 'product_id');
}

知道是什么原因导致了这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-08-10 05:26:46

我建议创建自己的枢轴模型并坦率地设置主键:

代码语言:javascript
复制
use Illuminate\Database\Eloquent\Relations\Pivot;

class ProductVariants extends Pivot
{
   protected $primaryKey = ['product_id', 'variant_id'];
public $incrementing = false;
}

在此之后,您应该修改您的关系:

代码语言:javascript
复制
public function variations()
    {
        return $this->belongsToMany(Variant::class)
                        ->using(ProductVariants::class);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63334416

复制
相关文章

相似问题

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