Postgresql自动增量在Ubuntu中不起作用

我有一张名为work_expenses的表:

                                                             Table "public.work_expenses"
       Column       |  Type   | Collation | Nullable |                        Default                         | Storage  | Stats target | Description 
--------------------+---------+-----------+----------+--------------------------------------------------------+----------+--------------+-------------
 work_expensesid    | integer |           | not null | nextval('work_expenses_work_expensesid_seq'::regclass) | plain    |              | 
 workid             | integer |           |          |                                                        | plain    |              | 
 employeeid         | integer |           |          |                                                        | plain    |              | 
 date_linkid        | integer |           |          |                                                        | plain    |              | 
 expense_categoryid | integer |           |          |                                                        | plain    |              | 
 cost_incl_gst      | numeric |           |          |                                                        | main     |              | 
 note               | text    |           |          |                                                        | extended |              | 
Indexes:
    "work_expenses_pkey" PRIMARY KEY,btree (work_expensesid)
Foreign-key constraints:
    "work_expenses_date_linkid_fkey" FOREIGN KEY (date_linkid) REFERENCES date_link(date_linkid)
    "work_expenses_employeeid_fkey" FOREIGN KEY (employeeid) REFERENCES employee(employeeid)
    "work_expenses_expense_categoryid_fkey" FOREIGN KEY (expense_categoryid) REFERENCES expense_category(expense_categoryid)
    "work_expenses_workid_fkey" FOREIGN KEY (workid) REFERENCES work(workid)

当我在不输入work_expensesid的情况下插入数据时,它应该自动递增,但不会自动递增。改为使用以下INSERT命令:

INSERT INTO work_expenses (work_expensesid,workid,employeeid,date_linkid,expense_categoryid,cost_incl_gst,note)
VALUES
        (NULL,1,220,4,5.00,NULL),(NULL,5,33.75,(631,218,13,21.50,'trailer load of tree branches')
;

我收到如下错误消息:

ERROR:  INSERT has more target columns than expressions
LINE 1: ...mployeeid,note)
                                                                  ^

所有其他表都存在此问题。有人能指出我正确的方向吗?我要达到的目的是,每次输入记录时都不需要提供新的序列号。该表当前具有365条记录,这些记录是通过提供唯一的work_expensesid输入的。 附加信息:

         Sequence "public.work_expenses_work_expensesid_seq"
  Type   | Start | Minimum |  Maximum   | Increment | Cycles? | Cache 
---------+-------+---------+------------+-----------+---------+-------
 integer |     1 |       1 | 2147483647 |         1 | no      |     1
Owned by: public.work_expenses.work_expensesid
Schema |               Name                |   Type   |  Owner   |    Size    | Description 
--------+-----------------------------------+----------+----------+------------+-------------
 public | work_expenses_work_expensesid_seq | sequence | postgres | 8192 bytes | 
crewdb=# select count(*) from work_expenses;
 count 
-------
   365
(1 row)
a714618777 回答:Postgresql自动增量在Ubuntu中不起作用

  

INSERT具有比表达式更多的目标列

这只是意味着您需要从插入目标列表中删除列work_expensesid

INSERT INTO work_expenses 
  (workid,employeeid,date_linkid,expense_categoryid,cost_incl_gst,note)
VALUES
  (NULL,1,220,4,5.00,NULL),(NULL,5,33.75,(631,218,13,21.50,'trailer load of tree branches');

Online example


建议现在使用身份列:

create table work_expenses
(
  work_expensesid     integer   not null generated always as identity primary key,workid              integer,employeeid          integer,date_linkid         integer,expense_categoryid  integer,cost_incl_gst       numeric,note                text   
)                            
本文链接:https://www.f2er.com/2966977.html

大家都在问