【发布时间】:2019-11-13 20:40:52
【问题描述】:
我从 PHP artisan 命令创建了迁移,它在我的 Postgres 数据库中创建了一个表,id 设置为 auto_increment。
我在 laravel 中做了一些播种机,通过 php artisan db:seed 命令将三行数据馈送到之前创建的表中。
当我在同一个表中通过某种形式插入数据时,它给了我一个错误。
error:SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "roles_pkey" DETAIL: Key (id)=(1) already exists. (SQL: insert into "roles" ("name", "guard_name", "updated_at", "created_at") values (staff, web, 2019-07-03 07:38:37, 2019-07-03 07:38:37) returning "id")
【问题讨论】:
-
你能发布你的迁移吗?
-
因为错误说 id=1 已经存在...确保您没有重复的行。提供一些代码来帮助你
-
如果您在 PostgreSQL 中手动为 INSERT 语句指定“auto_increment”列,则不会使用序列,因此将保持为“1”。我的猜测是,您用来生成随机数据的这个工具忽略了您将表配置为使用数据库内置身份机制并继续使用其他东西这一事实。
-
@jycr753
public function up() { Schema::create('themes', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('user_id'); $table->string('theme_code'); $table->string('theme_name'); $table->integer('sequence'); $table->timestamps(); }); }
标签: laravel postgresql