【问题标题】:Postgresql auto-increment is not working in UbuntuPostgresql 自动增量在 Ubuntu 中不起作用
【发布时间】:2020-03-30 20:39:49
【问题描述】:

我有一张名为 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,1,220,5,33.75,NULL),
        (631,1,218,13,21.50,'trailer load of tree branches')
;

我收到如下错误消息:

ERROR:  INSERT has more target columns than expressions
LINE 1: ...mployeeid,date_linkid,expense_categoryid,cost_incl_gst,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)

【问题讨论】:

    标签: postgresql auto-increment ddl


    【解决方案1】:

    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, 1,220,5,33.75,NULL),
      (631, 1,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   
    )                            
    

    【讨论】:

    • 抱歉,列数不正确。然后解决方案是删除对work_expensesid 列的引用
    • 我做到了,请参阅最后一条评论。它仍然不起作用。不过还是谢谢:-)
    • 那么您手动插入了一些没有推进序列的值。有关说明,请参阅 here。和here 寻求解决方案。使用 generated always 时,您不会意外地为此类列提供值。
    • 如何更改您对现有列的建议。我的意思是“始终作为身份主键生成不为空”。我的意思是在现有行的表上这样做安全吗?
    • 购买方式,您最后评论中的解决方案有效。非常感谢您的帮助!!!
    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 2015-11-22
    • 2013-05-25
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2017-03-09
    相关资源
    最近更新 更多