Postgres错误:列“id”中的空值 - 在插入操作期间

时间:2016-08-06 10:14:38

标签: sql postgresql yii null sql-insert

我使用postgresql和yii2框架。 好吧,我收到了一条非常有趣的错误信息:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 1, null, null, null, null, 1, Demo, , , , 1998-01-01, , , , 345345435453453, , , , , 1, , , f, f, f, f, 10, f, 1, f, f, f, null, null, null, 1470477479, 1470477479, null).

但是我检查了我的插入命令,那里没有“id”列!

INSERT INTO "advertiser" ("languages", "type", "name", "display_name", "title", "about", "birthday", "gender", "country_id", "county_id", "city_id", "city_part", "street", "house_number", "phone", "public_email", "public_url", "motto", "message", "im_facebook", "im_skype", "has_viber", "has_whatsapp", "has_sms_response", "visible_birthday", "is_checked", "status", "version", "user_id", "created_at", "updated_at") VALUES (NULL, 1, 'Demo', '', '', '', '1998-01-01', 1, NULL, NULL, NULL, '', '', '', '345345435453453', '', '', '', '', '', '', FALSE, FALSE, FALSE, FALSE, FALSE, 10, NULL, 1, 1470477479, 1470477479) RETURNING "id"

所以我真的无法理解这个错误信息。我没有发现Postgres或Yii尝试插入空ID值或什么。

顺便说一下,你可以找到结构

                                                    Table "public.advertiser"
        Column         |          Type          |            Modifiers            | Storage  | Stats target | Description 
-----------------------+------------------------+---------------------------------+----------+--------------+-------------
 id                    | integer                | not null                        | plain    |              | 
 user_id               | integer                |                                 | plain    |              | 
 country_id            | integer                |                                 | plain    |              | 
 county_id             | integer                |                                 | plain    |              | 
 city_id               | integer                |                                 | plain    |              | 
 district_id           | integer                |                                 | plain    |              | 
 type                  | smallint               |                                 | plain    |              | 
 name                  | character varying(255) | not null                        | extended |              | 
 display_name          | character varying(255) | default NULL::character varying | extended |              | 
 title                 | character varying(255) | default NULL::character varying | extended |              | 
 about                 | text                   |                                 | extended |              | 
 birthday              | date                   | not null                        | plain    |              | 
 city_part             | character varying(255) | default NULL::character varying | extended |              | 
 street                | character varying(255) | default NULL::character varying | extended |              | 
 house_number          | character varying(20)  | default NULL::character varying | extended |              | 
 phone                 | character varying(15)  | not null                        | extended |              | 
 public_email          | character varying(255) | default NULL::character varying | extended |              | 
 public_url            | character varying(255) | default NULL::character varying | extended |              | 
 motto                 | character varying(255) | default NULL::character varying | extended |              | 
 message               | text                   |                                 | extended |              | 
 gender                | smallint               | not null default 1              | plain    |              | 
 im_facebook           | character varying(255) | default NULL::character varying | extended |              | 
 im_skype              | character varying(255) | default NULL::character varying | extended |              | 
 has_viber             | boolean                | not null default false          | plain    |              | 
 has_whatsapp          | boolean                | not null default false          | plain    |              | 
 has_sms_response      | boolean                | not null default false          | plain    |              | 
 visible_birthday      | boolean                | not null default false          | plain    |              | 
 status                | smallint               | not null default 10             | plain    |              | 
 is_checked            | boolean                | not null default false          | plain    |              | 
 geo_latitude          | double precision       |                                 | plain    |              | 
 geo_longitude         | double precision       |                                 | plain    |              | 
 languages             | integer[]              |                                 | extended |              | 
 created_at            | integer                |                                 | plain    |              | 
 updated_at            | integer                |                                 | plain    |              | 
 version               | bigint                 | default 0                       | plain    |              | 
Indexes:
    "advertiser_pkey" PRIMARY KEY, btree (id)

你有什么建议?我应该在哪里寻找问题?

6 个答案:

答案 0 :(得分:18)

您没有为ComponentManager.getInstance().supportedSsh2CiphersCS().add( Ssh2Context.CIPHER_AES128_CBC, AES128Cbc.class); ComponentManager.getInstance().supportedSsh2CiphersSC().add( Ssh2Context.CIPHER_AES128_CBC, AES128Cbc.class); 插入值。由于您没有明确设置它,因此隐式地给出了id值,当然,这不是主键列的有效值。您可以通过将此列定义为null而不是普通的serial来避免这种情况,并将所有繁重的工作留给数据库。

答案 1 :(得分:2)

serial关键字在解析时被展开,之后无法看到。

Postgresql 10版本开始,有以下选择:

id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY

它应该符合SQL标准,因此与Oracle兼容。

有关更多详细信息,请参见此blog

答案 2 :(得分:0)

将现有主键更改为serial。阅读此内容以进行更改

Changing primary key int type to serial

答案 3 :(得分:0)

如果由于客户端,管理,数据库权限等原因而无法更改为serial

数据库可能正在使用sequence

这是您需要了解的内容:SELECT nextval('seq_nuu_filtreelement')

阅读:

我没有设法使pg_catalog.pg_get_serial_sequence('schema.table', 'id')工作。

因此,我在数据库浏览器中找到了序列,并使用命令:

SELECT nextval('seq_table_name')

答案 4 :(得分:0)

在将PostgreSQL10转储读入9.6数据库服务器后发生了。之后,将自动在丢失的位置创建顺序ID的序列。

可以这样显示(在psql中):

SELECT  column_name
,       column_default
FROM    information_schema.columns
WHERE   table_name = 'django_migrations'
ORDER BY 
    ordinal_position;

其中django_migrations是表名。它应该显示如下内容:

 column_name |                column_default                 
-------------+-----------------------------------------------
 id          | nextval('django_migrations_id_seq'::regclass)
[...]

如果“ column_default”中的值为空,则序列会丢失。

答案 5 :(得分:0)

如果由于某种原因,您无法更改架构以将 id 列的类型从当前更改为 serialize;然后,您可以将 id 与其他值一起插入,如下所示:

(select max(id) + 1 from table)