我必须更改数据库结构。我们有几个具有相同列(大多数)的表,现在我想将它移动到一个父表的子节点,然后将数据从旧的分散表移动到新的(继承的)。
因此,这两个表(继承 comments_temp 和主注释)具有相同的结构,但列顺序不同:
\d comments
Column | Type | Modifiers
------------+-----------------------------+---------------------------------------------------------------
comment_id | bigint | not null default nextval('comments_comment_id_seq'::regclass)
post_id | bigint | not null
auser_id | bigint | not null
dt | timestamp without time zone | not null
text | text | not null
is_deleted | smallint | default 0
parent | bigint | default 0
| default 0
\d comments_temp
Column | Type | Modifiers
------------+-----------------------------+--------------------------------------------------------------------
comment_id | bigint | not null default nextval('comments_base_comment_id_seq'::regclass)
auser_id | bigint | not null
dt | timestamp without time zone | not null
text | text | not null
is_deleted | smallint | default 0
parent | bigint | default 0
post_id | bigint | not null
Inherits: comments_base
插入失败是因为列的另一个顺序(尽管复制表中的列和新表中的列匹配)。
INSERT INTO comments_temp ( SELECT * FROM comments );
所以,我在INSERT上遇到错误:
ERROR: column "dt" is of type timestamp without time zone but expression is of type bigint
LINE 1: INSERT INTO comments_temp ( SELECT * FROM comments );
^
HINT: You will need to rewrite or cast the expression.
如果在PostgreSQL中列的顺序不同时如何通过select插入?我不想使用隐式列名。
答案 0 :(得分:1)
指定插入中的列:
INSERT INTO comments_temp(comment_id, auser_id, dt, text, is_deleted, parent, post_id)
SELECT comment_id, auser_id, dt, text, is_deleted, parent, post_id FROM comments;