简体   繁体   English

PostgreSQL - 如果我不在表名或字段周围添加双引号,我会收到 SQL 错误 [42P01] 或 [42703]

[英]PostgreSQL - I get SQL Error [42P01] or [42703] if I don't add double quotes around table names or Fields

I'm using DBeaver to write script for my PostgreSQL database.我正在使用 DBeaver 为我的 PostgreSQL 数据库编写脚本。

I have a PostgreSQL DB with Tables autogenerated by C#/EFCore (Microsoft ORM) - I receive SQL Error [42P01] if I don't add double quotes around table names when I cut and paste my ORM queries to DBeaver.我有一个 PostgreSQL 数据库,其中包含由 C#/EFCore(Microsoft ORM)自动生成的表 - 如果我在将 ORM 查询剪切并粘贴到 DBeaver 时没有在表名周围添加双引号,我会收到 SQL 错误 [42P01]。 I got [42703] for fields without double quotes.对于没有双引号的字段,我得到了 [42703]。 I do not have to add double quotes in C# code but it appears to be required in DBeaver?我不必在 C# 代码中添加双引号,但它似乎在 DBeaver 中是必需的?

example:例子:

  • select * from Dnp3PropertyBase => SQL Error [42P01] select * from Dnp3PropertyBase => SQL 错误 [42P01]

  • select * from "Dnp3PropertyBase" => OK, all results shown... select * from "Dnp3PropertyBase" => 好的,显示所有结果...

Does anybody know if I can change a parameter in DBeaver somewhere in order to enter table names and fields without double quotes?有人知道我是否可以在某处更改 DBeaver 中的参数以便输入不带双引号的表名和字段吗?

Note: Using DBeaver 22.3.2 (latest on 2023-01-11)注意:使用DBeaver 22.3.2(最新于2023-01-11)

Update After reading: Postgresql tables exists, but getting "relation does not exist" when querying阅读后更新: Postgresql表存在,但查询时出现“关系不存在”

show search_path => public, public, "$user" show search_path => public, public, "$user"

SELECT * FROM information_schema.tables => All tables are in public schema SELECT * FROM information_schema.tables => 所有表都在公共模式中

SELECT * FROM information_schema.columns => All columns are in public schema SELECT * FROM information_schema.columns => 所有列都在公共模式中

Question: How to be able to cut and paste my EFCore generated queries from Visual Studio output window to DBeaver query without having any errors regarding table names and field names?问题:如何能够将我的 EFCore 生成的查询从 Visual Studio output window 剪切并粘贴到 DBeaver 查询,而不会出现任何关于表名和字段名的错误?

First let me copy @a_horse_with_no_name comment:首先让我复制@a_horse_with_no_name 评论:

Unquoted names are folded to lower case in Postgres (and to uppercase in Oracle, DB2, Firebird, and many others).未加引号的名称在 Postgres 中折叠为小写(在 Oracle、DB2、Firebird 等中折叠为大写)。 So SomeTable is in fact stored as sometable (or SOMETABLE).所以SomeTable实际上是存储为sometable(或SOMETABLE)。 However quoted identifiers have to preserve the case and are case sensitive then.但是,引用的标识符必须保留大小写并且区分大小写。 So "SomeTable" is stored as SomeTable所以“SomeTable”存储为 SomeTable

Many peoples recommended me to go with snake case which I didn't want to go with initialy because all tables were auto generated by EF Core (Microsoft C# ORM).许多人推荐我使用 go 和snake case ,而我不想使用初始值 go,因为所有表都是由 EF Core(Microsoft C# ORM)自动生成的。 I told myself that Microsoft would do standard things.我告诉自己,微软会做标准的事情。 Microsoft use the exact "class" name in code as the table name, by default.默认情况下,Microsoft 在代码中使用确切的“类”名称作为表名。 That appears to me very logical in order to stay coherent and apply the same rules everywhere.在我看来,为了保持连贯性并在所有地方应用相同的规则,这似乎非常合乎逻辑。 C# recommended to use Camel case for classes so each table names end by default in Camel case instead of snake case. C# 建议对类使用 Camel 大小写,这样每个表名称默认以 Camel 大小写而不是蛇形大小写结尾。

PostgreSQL seems to promote users to use snake casing because they lower case every non double quoted names. PostgreSQL 似乎提倡用户使用蛇形套管,因为他们将每个非双引号的名称小写。 According to a_horse_with_no_name, and I think the same, only PostgreSQL has the behavior of lower casing down every table names and field names which are not double quoted in SQL script.根据 a_horse_with_no_name,我认为相同,只有 PostgreSQL 具有在 SQL 脚本中没有双引号的每个表名和字段名下的小写行为。 That behavior (changing casing for non double quoted names) appears to me as being very limitative.这种行为(改变非双引号名称的大小写)在我看来是非常有限的。 It also has hidden effect that could be hard to find for non initiated peoples coming from other DB world.它还具有隐藏的效果,对于来自其他 DB 世界的非启蒙人群来说可能很难找到。

According toPostgreSQL doc , they recommend to use nuget package (.UseSnakeCaseNamingConvention()).根据PostgreSQL 文档,他们建议使用 nuget package (.UseSnakeCaseNamingConvention())。 It probably works fine for TPH (table per hierarchy) which is recommended by Microsoft for performance.它可能适用于 Microsoft 为提高性能而推荐的 TPH(每个层次结构的表)。 But it does not works for table name for TPC (table per class) because of actual bugs in EFCore 7 ( see Github project ).但它不适用于 TPC 的表名(每个类的表),因为 EFCore 7 中存在实际错误(请参阅 Github 项目)。

I received that message at the end of "update-database":我在“更新数据库”结束时收到了这条消息:

Both 'WindTurbine' and 'ResourceGenerator' are mapped to the table 'resource_generator'. “WindTurbine”和“ResourceGenerator”都映射到表“resource_generator”。 All the entity types in a non-TPH hierarchy (one that doesn't have a discriminator) must be mapped to different tables.非 TPH 层次结构(没有鉴别器的层次结构)中的所有实体类型都必须映射到不同的表。 See https://go.microsoft.com/fwlink/?linkid=2130430 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2130430

PostgreSQL doc : TPH supported OK but not for table in TPC (2023-01-12). PostgreSQL 文档:TPH 支持正常但不支持 TPC 中的表 (2023-01-12)。 I use TPC then I had to force each table name directly through TableAttribute.我使用 TPC,然后我不得不直接通过 TableAttribute 强制每个表名。

My solution For table name, I use snake casing by manually add a "Table" attribute to each of my classes with the proper name like this sample:我的解决方案对于表名,我使用蛇形外壳,通过手动向我的每个类添加一个“表”属性,并使用正确的名称,如下例所示:

[Table("water_turbine")]
public class WaterTurbine : ResourceGenerator

For fields, I use the EFCore.NamingConventions NugetPackage which works fine for fields names.对于字段,我使用 EFCore.NamingConventions NugetPackage,它适用于字段名称。 Don't forget that if you have 2 classes mapped to the same object, it is because you are using TPC and did not force table name through TableAttribute.不要忘记,如果您有 2 个类映射到同一个 object,那是因为您使用的是 TPC,并且没有通过 TableAttribute 强制表名。

This way all my table and fields names are snake casing and I can cut and paste any query dumped in my debugger directly in any SQL script window of DBeaver (or any SQL tool).这样,我所有的表和字段名称都是蛇形外壳,我可以将调试器中转储的任何查询直接剪切并粘贴到 DBeaver 的任何 SQL 脚本 window(或任何 SQL 工具)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL 错误:42P01:关系“[表]”不存在 - PostgreSQL ERROR: 42P01: relation “[Table]” does not exist Prisma PostgreSQL queryRaw 错误代码 42P01 表不存在 - Prisma PostgreSQL queryRaw error code 42P01 table does not exist CTE错误-错误代码:0,SQL状态:42P01 - CTE Error - Error Code: 0, SQL State: 42P01 错误SQL状态:postgres中为42P01 - Error SQL state: 42P01 In postgres 为什么 PSQL 创建表返回 [42P01] 错误:关系不存在 - Why PSQL create table returns [42P01] ERROR: relation does not exist 错误:关系“ shemaname.trame”不存在SQL状态:42P01 - ERROR: relation “shemaname.trame” does not exist SQL state: 42P01 Npgsql.PostgresException:'42P01:关系“表”不存在” - Npgsql.PostgresException: '42P01: relation “table” does not exist' 创建SQL表时,出现此错误“错误代码30000,SQL状态42X01:语法错误:在第3行,第22列遇到了“ AUTO_INCREMENT”。 - I get this error “Error code 30000, SQL state 42X01: Syntax error: Encountered ”AUTO_INCREMENT“ at line 3, column 22.” when I create a sql table SQL错误,我不明白 - SQL error, I don't get it Python:为什么 PostgreSQL 表中的列名用双引号括起来? - Python: Why are the column names in PostgreSQL table wrapped in double quotes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM