laravel 在迁移中设置 postgresql 标识列

laravel set postgresql identity column in migration

laravel $table->id() 将设置默认值 nextval('tablename_id_seq'::regclass)

如何设置 GENERATED BY DEFAULT AS IDENTITY

CREATE TABLE color (
    color_id INT GENERATED BY DEFAULT AS IDENTITY,
    color_name VARCHAR NOT NULL
);

引用the current documentation regarding migrations:

中的索引修饰符table
->generatedAs($expression) Create an identity column with specified sequence options (PostgreSQL).

以下代码

Schema::create('color', function (Blueprint $table) {
    $table->mediumInteger('color_id')->generatedAs();
    $table->string('color_name');
});

生成

create table "color" (
  "color_id" integer generated by default as identity not null,
  "color_name" varchar(255) not null
)

如果在 mediumInteger('color_id')generatedAs() 之间添加 ->nullable(),则 color_id 列改为:

"color_id" integer generated by default as identity null