【问题标题】:Rename nextval('...') in Postgres在 Postgres 中重命名 nextval('...')
【发布时间】:2015-04-14 23:04:02
【问题描述】:

我有一个名为 pivot_device_user 的表,并且在 ID 上有一个序列为 not null default nextval('pivot_device_user_id_seq'::regclass)

然后我决定将我的表重命名为pivot_box_user,但nextval(...) 仍然是nextval('pivot_device_user_id_seq'::regclass)

我想把它改成nextval('pivot_box_user_id_seq'::regclass)。我该怎么做?

【问题讨论】:

  • 否,因为该序列实际上是授予用户的权限。所以目前postgres返回一个错误,说用户没有正确的权限。

标签: postgresql ddl column-defaults


【解决方案1】:

首先您必须了解serial 的真正含义:

列默认值实际上并未存储为文本字面量。你看到的只是人类可读的文本表示:nextval('pivot_device_user_id_seq'::regclass)

'pivot_device_user_id_seq'::regclass 在内部被解析为OID(准确地说是regclass)——底层序列的OID——这就是实际存储的内容(早期绑定)。如果重命名序列,其 OID 保持不变。所以你需要做的就是rename the sequence:

ALTER SEQUENCE pivot_device_user_id_seq RENAME TO pivot_box_user_id_seq;

检查成功与否:

SELECT pg_get_serial_sequence('pivot_box_user', 'id');

相关:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2016-09-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2018-06-29
    • 2010-10-05
    • 1970-01-01
    相关资源
    最近更新 更多