【问题标题】:laravel set postgresql identity column in migrationlaravel 在迁移中设置 postgresql 标识列
【发布时间】:2021-09-29 15:04:28
【问题描述】:

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
);

【问题讨论】:

    标签: laravel postgresql migration


    【解决方案1】:

    引用the current documentation regarding migrations:中的索引修饰符表

    ->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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-22
      • 2015-09-01
      • 2016-10-09
      • 2021-06-11
      • 2018-11-23
      • 2016-06-07
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多