【问题标题】:Cannot create a table due to naming conflict由于命名冲突,无法创建表
【发布时间】:2014-03-12 06:42:35
【问题描述】:

我试图在我的数据库中创建一个表,它给了我以下错误。

ERROR:  type "tbl_last_alert" already exists
HINT:  A relation has an associated type of the same name, so you must use a name that doesn't conflict with any existing type.

然后我认为该表必须存在所以我运行了以下查询:

select * from pg_tables;

但是找不到任何东西。然后我尝试了:

select * from tbl_last_alert;
ERROR:  relation "tbl_last_alert" does not exist

知道如何排序吗?

我想通过

重命名类型
ALTER TYPE v4report.tbl_last_alert RENAME TO tbl_last_alert_old;
ERROR:  v4report.tbl_last_alert is a table's row type
HINT:  Use ALTER TABLE instead.

并得到错误。

【问题讨论】:

    标签: sql postgresql database-administration


    【解决方案1】:

    Postgres 为每个表创建一个具有相同名称的复合类型。这就是错误消息提到“类型”而不是“表”的原因。实际上,表名不能与:

    r = 普通表,i = 索引,S = 序列,v = 视图,m = 物化视图,c = 复合类型,t = TOAST 表,f = 外部表

    引用来自pg_class 上的手册。大胆强调我的。因此,您可以找到与此查询有冲突的条目:

    SELECT n.nspname AS schemaname, c.relname, c.relkind
    FROM   pg_class c
    JOIN   pg_namespace n ON n.oid = c.relnamespace
    WHERE  relname = 'tbl_last_alert';
    

    这涵盖了所有个可能的竞争对手,而不仅仅是类型。请注意,同一名称​​可以在多个架构中多次存在 - 但不能在同一个架构中。

    治愈

    如果您发现有冲突的复合类型,您可以重命名或删除它以让路 - 如果您不需要它!

    DROP TYPE tbl_last_alert;
    

    确保该类型的架构是搜索路径中的第一个匹配项或架构限定名称。我将架构添加到上面的查询中。喜欢:

    DROP TYPE public.tbl_last_alert;
    

    【讨论】:

    • drop type v4report.tbl_last_alert; ERROR: cache lookup failed for relation 19108 得到错误任何想法
    • ` 模式名 |相对名称 | relkind ------------+---------+--------- (0 行) `我得到了这个
    • @smn_onrocks:恐怕您的数据库中有些东西严重损坏了。您可以通过重新索引系统表来修复它。 Details in this related answer.这根可能是硬件问题。
    • @smn_onrocks 在您做任何其他事情之前,请遵循以下建议:wiki.postgresql.org/wiki/Corruption
    • @Erwin Brandstetter 已经尝试了您的所有建议,但仍然出现错误。
    【解决方案2】:

    如果不能删除类型,请从 pg_type 中删除:

    DELETE FROM pg_type where typname~'tbl_last_alert';
    

    【讨论】:

      【解决方案3】:

      错误将其称为“类型”;您可能在某处有一个具有该名称的类型。

      psql 中使用它来找出什么是什么:

      \d tbl_last_alert
      

      【讨论】:

        【解决方案4】:

        您可以在pg_type 表中查看:

        select * from pg_type where typname = 'tbl_last_alert'
        

        【讨论】:

        • select * from pg_type where typname = 'tbl_last_alert'; tbl_last_alert | 16716 | 10 | -1 | f | c | C | f | t | , | 19108 | 0 | 19109 | recor d_in | record_out | record_recv | record_send | - | - | - | d | x | f | \x1A | 0 | -1 | 0 | 0 | | 运行查询后我得到了这个现在我想删除表怎么做
        • @smn_onrocks drop type tbl_last_alert.
        • drop type v4report.tbl_last_alert; ERROR: cache lookup failed for relation 19108 得到错误任何想法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-19
        • 2018-04-11
        • 2012-12-01
        • 1970-01-01
        相关资源
        最近更新 更多