SQL PostgreSQL 错误:42P01:关系“[表]”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26631205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:52:51  来源:igfitidea点击:

PostgreSQL ERROR: 42P01: relation "[Table]" does not exist

sqldatabasepostgresqlpostgis

提问by Rahul Vijay Dawda

I'm having this strange problem using PostgreSQL 9.3 with tables that are created using qoutes. For instance, if I create a table using qoutes:

我在使用 PostgreSQL 9.3 和使用 qoutes 创建的表时遇到了这个奇怪的问题。例如,如果我使用 qoutes 创建一个表:

create table "TEST" ("Col1" bigint);

the table is properly created and I can see that the quotes are preserved when view it in the SQL pane of pgAdminIII. But when I query the DB to find the list of all available tables (using the below query), I see that the result does not contain quotes around the table name.

该表已正确创建,我可以看到在 pgAdminIII 的 SQL 窗格中查看时保留了引号。但是当我查询数据库以查找所有可用表的列表(使用下面的查询)时,我看到结果不包含表名周围的引号。

select table_schema, table_name from information_schema.tables where not table_schema='pg_catalog' and not table_schema='information_schema';

Since the table was created with quotes, I can't use the table name returned from the above query directly since it is unquoted and throws the error in posted in the title.

由于该表是用引号创建的,我不能直接使用从上述查询返回的表名,因为它没有被引用并在标题中发布错误。

I could try surrounding the table names with quotes in all queries but I'm not sure if it'll work all the time. I'm looking for a way to get the list of table names that are quoted with quotes in the result.

我可以尝试在所有查询中用引号括起表名,但我不确定它是否会一直工作。我正在寻找一种方法来获取在结果中用引号引用的表名列表。

I'm having the same issue with column names as well but I'm hoping that if I can find a solution to the table names issue, a similar solution will work for column names as well.

我对列名也有同样的问题,但我希望如果我能找到表名问题的解决方案,类似的解决方案也适用于列名。

回答by Hans-Jürgen Sch?nig

you have two choices: - no quotes: then everything will automatically be lowercase and non-case-sensitive - with quotes: from now on everything is case sensitive.

您有两个选择: - 不加引号:那么所有内容都将自动变为小写且不区分大小写 - 使用引号:从现在开始,所有内容都区分大小写。

i would highly recommend to NOT use quotes and make PostgreSQL behave non case sensitive. it makes life so much easier. once you get into quoting you got to use it EVERYWHERE as PostgreSQL will start to be very precise.

我强烈建议不要使用引号并使 PostgreSQL 的行为不区分大小写。它让生活变得更轻松。一旦开始引用,就必须在任何地方使用它,因为 PostgreSQL 将开始变得非常精确。

some example:

一些例子:

   TEST = test       <-- non case sensitive
   "Test" <> Test    <-- first is precise, second one is turned to lower case
   "Test" = "Test"   <-- will work
   "test" = TEST     <-- should work; but you are just lucky.

really try to avoid this kind of trickery at any cost. stay with 7 bit ascii for object names.

真的要不惜一切代价避免这种诡计。对象名称使用 7 位 ascii。

回答by Mike T

A string function used to suitably quote identifiers in an SQL statement string is quote_ident(), which references a good example(used in conjunction with related quote_literal()).

用于在 SQL 语句字符串中适当引用标识符的字符串函数是quote_ident(),它引用了一个很好的示例(与相关 结合使用quote_literal())。

To use your example, and mix in other results:

要使用您的示例,并混合其他结果:

select
   quote_ident(table_schema) as table_schema,
   quote_ident(table_name) as table_name
...

 table_schema |    table_name
--------------+------------------
 ...
 public       | good_name
 public       | "table"
 public       | some_table
 public       | "something else"
 public       | "Tom's work"
 public       | "TEST"
 ...

回答by Wael Al Wirr

While using npg package as your data store ORM you are expecting the ORM framework (Entity Framework in our case) to generate the sql statement you might face a PostgreSQL exception the relation 'Table Name' does not exist

使用 npg 包作为数据存储 ORM 时,您希望 ORM 框架(在我们的示例中为实体框架)生成 sql 语句,您可能会遇到 PostgreSQL 异常,关系“表名称”不存在

Either the table is not created or the generated SQL statement is missing something. Try to debug using visual studio you will see that the schema name is not leading the table name

表未创建或生成的 SQL 语句缺少某些内容。尝试使用 Visual Studio 进行调试,您将看到架构名称未引导表名称

SELECT "ID", "Name", "CreatedBy", "CreatedDate" 
FROM "TestTable"; 

while PostgreSQL is expecting the schema name. Resolution is in the DBContext class override the OnModelCreating method and add modelBuilder.HasDefaultSchema("SchemaName");and execute the base constructor which should look like the following

而 PostgreSQL 需要模式名称。解决方法是在 DBContext 类中覆盖 OnModelCreating 方法并添加modelBuilder.HasDefaultSchema("SchemaName");和执行基本构造函数,它应如下所示

protected override void OnModelCreating(ModelBuilder modelBuilder)   {             
  modelBuilder.HasDefaultSchema("PartyDataManager");                  
  base.OnModelCreating(modelBuilder);         
}