无法将表重命名为与类型相同的名称在postgres中。

huangapple go评论65阅读模式
英文:

Can't rename table to the same name as type in postgres

问题

无法执行需要将表重命名为已存在的类型相同名称的SQL查询。在PostgreSQL中,我没有找到关于不同对象具有相同名称的任何规则或注释。

CREATE TYPE ratings.processed_sport_bet AS ENUM ('sport_bet_created', 'sport_bet_resulted');

ALTER TABLE ratings.processed_sport_bet_resulted RENAME TO processed_sport_bet;

获得错误信息:

[42710] ERROR: 类型 "processed_sport_bet" 已经存在
英文:

I can't execute sql query, that has to rename the table to the same name as type, that already exists. I have not found any rules or comments about the same name for different things in postgres.

CREATE TYPE ratings.processed_sport_bet AS ENUM ('sport_bet_created', 'sport_bet_resulted');

ALTER TABLE ratings.processed_sport_bet_resulted RENAME TO processed_sport_bet;

Get the error

[42710] ERROR: type "processed_sport_bet" already exists

答案1

得分: 2

当创建表时,PostgreSQL会自动创建一个与表的行结构同名的类型。当重命名表时,相关联的类型也会被重命名。如果另一个类型已经使用了相同的名称,那么会引发异常。

英文:

When a table is created, PostgreSQL automatically creates a type for the table's row structure, with the same name as the table. When renaming the table, the associated type is also renamed. If another type already has that name, then an exception is raised.

答案2

得分: 0

CREATE TABLE 会自动创建一个表示表中一行对应的复合类型的数据类型。因此,在同一模式中,表不能与任何现有数据类型具有相同的名称。参考链接:https://www.postgresql.org/docs/15/sql-createtable.html

英文:

CREATE TABLE also automatically creates a data type that represents the composite type corresponding to one row of the table. Therefore, tables cannot have the same name as any existing data type in the same schema. For ref: https://www.postgresql.org/docs/15/sql-createtable.html

huangapple
  • 本文由 发表于 2023年6月8日 14:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定