【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed:SQLSTATE [23000]:完整性约束违规:19 CHECK 约束失败:
【发布时间】:2020-12-28 04:16:52
【问题描述】:

我正在使用 laravel 7.2 进行一个项目,由于某些原因,我决定使用一个 sqlite 数据库,这无论如何都不错,但事实是,我对 slite 数据库知之甚少。再加上一切都很好,直到出现Integrity constraint violation: 19 CHECK constraint failed: 错误。 好吧,我认为是我的代码 “当然仍然可能” 导致了错误,直到我尝试手动将数据插入数据库表并得到

我尝试从控制器插入数据

Merge::create([
       'user_id' => $query->user_id,
       'merge_id' => $to_be_merged->id,
       'investment_id' => $query->id,
       'file' => null,
       'merge_status' => 'pending',
       'merged_user_action' => 'pending',
       'merge_expiry' => Carbon::now()->addDay()
   ]);

这导致了这个错误

SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: cashflow_merges (SQL: insert into "cashflow_merges" ("user_id", "merge_id", "investment_id", "file", "merge_status", "merged_user_action", "merge_expiry", "updated_at", "created_at") values (21, 47, 7, ?, pending, pending, 2020-09-10 16:23:41, 2020-09-09 16:23:41, 2020-09-09 16:23:41))

然后,CHECK 约束又出现了错误。仍在试图弄清楚这意味着什么。

迁移

 Schema::create('cashflow_merges', function (Blueprint $table) {
     $table->increments('id');
     $table->integer('user_id')->unsigned();
     $table->integer('merge_id')->unsigned();
     $table->integer('investment_id')->unsigned();
     $table->string('file')->nullable();
     $table->enum('merge_status', ['pending', 'denied', 'complete', 'canceled', 'expired']);
     $table->enum('merged_user_action', ['waiting', 'seen', 'expired']);
     $table->string('merge_expiry');
     $table->timestamps();
  });

【问题讨论】:

  • 不确定,但可能是这个错误'file' => null, 给一个值再试一次
  • @sta 我已经做到了,还是一样,此外,文件可以为空
  • 您是否在模型的 $fillable 属性上添加了 file
  • @sta protected $guarded = [] 我猜
  • 好的,我刚刚收到一个错误:说数据库已锁定

标签: php sql laravel sqlite eloquent


【解决方案1】:

在您的迁移中,merged_user_action 字段没有pending 枚举,但您通过create statement 插入pending。您只需要使用定义的枚举值中的值。

【讨论】:

  • 待定不在迁移中,但我后来添加了它,但问题仍然存在。我不得不切换到 mysql 并进行相同的更改,一切都很好!
猜你喜欢
  • 2021-01-26
  • 2020-01-28
  • 2018-03-20
  • 2021-06-10
  • 2021-03-24
  • 1970-01-01
  • 2020-09-17
  • 2022-11-02
  • 2014-08-18
相关资源
最近更新 更多