【问题标题】:SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value Laravel 8SQLSTATE [HY000]:一般错误:1364 字段 'user_id' 没有默认值 Laravel 8
【发布时间】:2021-03-22 14:50:43
【问题描述】:

SQLSTATE[HY000]:一般错误:1364 字段“user_id”没有默认值(SQL:插入到productspricecategory_idupdated_atcreated_at)值( 20, 3, 2020-12-11 07:00:34, 2020-12-11 07:00:34))

我有什么错?

产品表:

Schema::create('products', function (Blueprint $table) {
        $table->increments("id");
        $table->string("product_name")->default('');
        $table->decimal("price" ,5,2);
        $table->string("photo", 100)->default('');
        $table->text("description")->nullable();
        $table->integer("user_id")->unsigned();
        $table->integer("category_id")->unsigned();
        $table->timestamps();

        $table->foreign("user_id")->references("id")->on("users")->cascadeOnUpdate()->cascadeOnDelete();
        $table->foreign("category_id")->references("id")->on("categories")->cascadeOnUpdate()->cascadeOnDelete();
    });

ProductController 存储函数。

public function store(Request $request)
{
    $request->validate([
        "product_name"=>"required",
        "price"=>"required|integer",
        "user_id"=>"required|integer",
        "category_id"=>"required|integer",
    ]);

    $product = new Product([
        "product-name" => $request->get("product_name"),
        "price" => $request->get("price"),
        "user_id" => $request->get("user_id"),
        "category_id" => $request->get("category_id"),
    ]);

    $product->save();

    return redirect("products")->with("success","Products added.");
}

型号:

protected $table = 'products';
protected $fillable = ['name', 'price', 'photo', 'description', 'created_by', 'category_id'];

【问题讨论】:

  • Welcome to So ... 检查模型中的可归档属性
  • 您好,先生,我在帖子中添加了产品模型。你能帮我检查一下吗?
  • $fillable = ['user_id'] 添加user_id
  • 我添加了,但我有一个新错误。 SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(bootcamp.products,CONSTRAINT products_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (@987654336 @) ON DELETE CASCADE ON UPDATE CASCADE)
  • 从迁移中移除 cascadeOnUpdate() 并重新运行迁移

标签: php laravel


【解决方案1】:

根据eloquent mass assignment documentation,您应该设置类的可填充属性,以便能够使用createupdate方法。

您可以将 user_id 放入 Product 类的 fillable 数组中,或将 guarded 数组设置为空数组。

Product 
{
    fillable=[
        'user_id',
        ... // other attributes

    ];
}

或者

Product 
{
    guarded = [];
}

【讨论】:

  • 我添加了这个,但我有一个新错误。 SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(bootcamp.products,CONSTRAINT products_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (@987654333 @) ON DELETE CASCADE ON UPDATE CASCADE)
  • 您不应该在级联上同时操作UpdateDelete。删除其中一个。
  • Mysql cascade on delete reference 了解有关 on cascade 功能的更多信息。当您删除父表上的记录时,它会自动从子表中删除数据。例如,当您删除一个用户时,它会删除通过user_id 引用它的产品。
  • 我删除了更新操作,但我又遇到了同样的错误
  • 你跑migrate:fresh了吗?
【解决方案2】:

当你使用这个方法时

$product = new Product([
    //array
]);

它实际上是在“填充”你的模型。

同 $产品=新产品(); $product->fill($array);

选项 1

正确更新您的模型 $fillable 以包含 user_id 或替换为受保护的,并且只添加您不希望能够填充的列。 Guarded 是我个人最喜欢的,因为如果您向数据库中添加额外的列,您不必手动将每一列都添加为可填充的。

选项 2

保护 user_id 是个好主意,特别是如果你这样做

$product = new Product([
    $request->all()
]);

因为用户可以破解自己的输入并更改他们不应该更改的内容。

应该像这样手动设置 user_id 等列

$product = new Product([
    $request->all();
);

$product->user_id = $request->get("user_id");

【讨论】:

  • 我做了这个改变。但是我得到了不同的错误。 SQLSTATE [23000]:完整性约束违规:1048 列“category_id”不能为空(SQL:插入productsproduct_namepricecategory_idupdated_atcreated_at)值(Iphone 11 , 1, ?, 2020-12-11 10:23:38, 2020-12-11 10:23:38))。我为 category_id 添加了可为空的运算符,但之后我在照片列中遇到了同样的错误。
【解决方案3】:

在您的数据库中,列名是“product_name”。但在您的模型中,您的fillable =['name'.....] 。将其更改为“产品名称”。回答这个问题有点晚了,但我希望其他人会觉得它有帮助。使用protected $guarded = []; 或先检查您的可填写内容。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2020-05-25
    • 2017-11-10
    • 1970-01-01
    • 2021-06-05
    • 2019-01-05
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多