我有一张称为时间表的表。
Table "public.timesheet"
Column | Type | Collation | Nullable | Default
---------------------+------------------------+-----------+----------+------------------------------
timesheetid | integer | | not null | generated always as identity
employeeid | integer | | |
date_linkid | integer | | |
employee_functionid | integer | | |
start_time | time without time zone | | |
finish_time | time without time zone | | |
note | text | | |
Indexes:
"timesheet_pkey" PRIMARY KEY, btree (timesheetid)
Check constraints:
"timesheet_time_check" CHECK (finish_time > start_time)
Foreign-key constraints:
"timesheet_date_linkid_fkey" FOREIGN KEY (date_linkid) REFERENCES date_link(date_linkid)
"timesheet_employee_functionid_fkey" FOREIGN KEY (employee_functionid) REFERENCES employee_function(employee_functionid)
"timesheet_employeeid_fkey" FOREIGN KEY (employeeid) REFERENCES employee(employeeid)
当我运行以下命令时:
--INSERT TIMESHEET INFORMATION:
INSERT INTO timesheet (timesheetid,employeeid,date_linkid,employee_functionid,start_time,finish_time,note)
VALUES
(DEFAULT,1,223,1,'13:56:00','16:40:00',NULL),
(DEFAULT,1,223,3,'18:10:00','18:19:00',NULL)
;
我收到以下错误消息:
ERROR: cannot insert into column "timesheetid"
DETAIL: Column "timesheetid" is an identity column defined as GENERATED ALWAYS.
HINT: Use OVERRIDING SYSTEM VALUE to override.
我不知道哪里出了问题,因为我在执行以下命令后再次尝试了此操作,但仍然收到相同的错误消息。有人知道这里出了什么问题吗?
crewdb=# SELECT MAX(timesheetid) FROM timesheet;
max
-----
418
(1 row)
crewdb=# SELECT nextval('timesheet_timesheetid_seq');
nextval
---------
419
(1 row)
crewdb=# BEGIN;
BEGIN
crewdb=# SELECT setval('timesheet_timesheetid_seq', COALESCE((SELECT MAX(timesheetid)+1 FROM timesheet), 1), false);
setval
--------
419
(1 row)
crewdb=# COMMIT;
COMMIT
答案 0 :(得分:2)
如果timesheetid
是自动生成的列,则应该可以从插入的列列表中完全省略它:
INSERT INTO timesheet (employeeid, date_linkid, employee_functionid, start_time,
finish_time, note)
VALUES
(1, 223, 1, '13:56:00', '16:40:00', NULL),
(1, 223, 3, '18:10:00', '18:19:00', NULL);
Postgres将自动为timesheetid
生成值。