postgresql 生成nil id,定义了id序列,为什么?

hjzp0vay  于 2023-05-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(112)

我有一个名为async_data的表,它的id列也定义了自动增量。但是在生产环境中,我看到一些插入查询在说

PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint

Rails迁移文件

class CreateAsyncData < ActiveRecord::Migration[5.0]
  def change
    create_table :async_data do |t|
      t.integer :request_id
      t.integer :sourced_contact_id
      t.integer :data_source_id
      t.boolean :is_enriched
      t.column  :requested_params, :json
      t.text :q
      t.datetime :fetched_at
      t.timestamps
    end
  end
end
CREATE TABLE public.async_data (
        id integer NOT NULL,
        request_id integer,
        sourced_contact_id integer,
        data_source_id integer,
        is_enriched boolean DEFAULT false,
        requested_params json,
        fetched_at timestamp without time zone,
        created_at timestamp without time zone NOT NULL,
        updated_at timestamp without time zone NOT NULL,
        q text,
        is_processed boolean DEFAULT false NOT NULL,
        is_data_pushed boolean DEFAULT false NOT NULL
    );

\d async_data;

Table "public.async_data"
      Column       |            Type             | Collation | Nullable |                      Default                       
-------------------+-----------------------------+-----------+----------+----------------------------------------------------
 id                | integer                     |           | not null | nextval('async_data_id_seq'::regclass)
 request_id        | integer                     |           |          | 
 source_company_id | integer                     |           |          | 
 data_source_id    | integer                     |           |          | 
 is_enriched       | boolean                     |           |          | 
 requested_params  | json                        |           |          | 
 q                 | text                        |           |          | 
 fetched_at        | timestamp without time zone |           |          | 
 created_at        | timestamp without time zone |           | not null | 
 updated_at        | timestamp without time zone |           | not null | 
Indexes:
    "async_data_pkey" PRIMARY KEY, btree (id)
--
-- Name: async_data_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.async_data_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

我想在开发环境中重新生成相同的代码,并想知道为什么生成id nil。

8cdiaqws

8cdiaqws1#

我只是有同样的问题,不知道为什么,但构建抛出一个错误的序列创建,看起来它不应该有“作为整数”
我不确定这是否与数据库客户端版本有关,但只是删除“As integer”对我来说是修复方法。

相关问题