为什么 PeeWee 引用 table 个名字?
Why does PeeWee quote table names?
我正在尝试将 PeeWee 与 SQL 中继集成,我已经 运行 解决了 PeeWee 创建 SQL 语句并在 table 名称周围使用双引号的问题.
这是一些示例代码
from peewee import BooleanField
from peewee import CharField
from peewee import DateField
from peewee import ForeignKeyField
from peewee import Model
from SQLRelay import PySQLRDB
from sqlrelay_ext import SQLRelayDatabase
DB = SQLRelayDatabase('test2', host='<hostname>', user='<username>', password='<password>')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = DB
class Pet(Model):
owner = ForeignKeyField(Person, backref='pets')
name = CharField()
animal_type = CharField()
class Meta:
database = DB
DB.connect()
Person.create_table(safe=False)
Pet.create_table(safe=False)
这是一个示例堆栈跟踪。
Query: CREATE TABLE "person" ("id" INTEGER NOT NULL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "birthday" DATE NOT NULL, "is_relative" SMALLINT NOT NULL)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2576, in execute_sql
cursor.execute(sql, params or ())
File "/usr/local/lib/python3.6/site-packages/SQLRelay/PySQLRDB.py", line 177, in execute
raise DatabaseError('<pre>%s</pre>' % the_error)
SQLRelay.PySQLRDB.DatabaseError: <pre>Server message: Incorrect syntax near 'person'. severity(0) number(102) state(1) line(1) Server Name:ubuntu-mssql Procedure Name:</pre>
虽然我知道这是 ANSI 标准,但不幸的是,许多数据库客户端都没有很好地支持它。 PeeWee 这样做有什么具体原因吗?是否可以创建可以排除双引号的自定义数据库适配器?
如有任何帮助,我们将不胜感激。
SQL中继我不熟悉。 Peewee 引用 table 个名称(和其他实体)有几个原因:
- 它得到 peewee 开箱即用的数据库的良好支持。
- 因此您可以使用 SQL 保留字作为标识符或别名
- 如果您的 table/column 包含空格( 已经报道 信不信由你...人们会做一些奇怪的事情)。
如果不保留所有 SQL 保留字的相当全面的列表,Peewee 将不知道什么时候引号是严格要求的,什么时候是可选的。不久前在 GH 问题中讨论过这个问题。我选择不实施条件引用,因为我不想维护一个特例加上一大堆保留字。
您可以覆盖数据库上的 execute_sql() 方法-class 并使用正则表达式去除所有引号,然后再将它们发送到数据库驱动程序,但这感觉有点古怪。您还可以覆盖数据库 get_sql_context() 方法以提供您自己的 SQL 生成上下文,它也可能处理取消引用的标识符。
编辑:看起来您正在使用 sql 服务器?你可以试试转 "SET QUOTED_IDENTIFIER": https://docs.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017
我正在尝试将 PeeWee 与 SQL 中继集成,我已经 运行 解决了 PeeWee 创建 SQL 语句并在 table 名称周围使用双引号的问题.
这是一些示例代码
from peewee import BooleanField
from peewee import CharField
from peewee import DateField
from peewee import ForeignKeyField
from peewee import Model
from SQLRelay import PySQLRDB
from sqlrelay_ext import SQLRelayDatabase
DB = SQLRelayDatabase('test2', host='<hostname>', user='<username>', password='<password>')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = DB
class Pet(Model):
owner = ForeignKeyField(Person, backref='pets')
name = CharField()
animal_type = CharField()
class Meta:
database = DB
DB.connect()
Person.create_table(safe=False)
Pet.create_table(safe=False)
这是一个示例堆栈跟踪。
Query: CREATE TABLE "person" ("id" INTEGER NOT NULL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "birthday" DATE NOT NULL, "is_relative" SMALLINT NOT NULL)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2576, in execute_sql
cursor.execute(sql, params or ())
File "/usr/local/lib/python3.6/site-packages/SQLRelay/PySQLRDB.py", line 177, in execute
raise DatabaseError('<pre>%s</pre>' % the_error)
SQLRelay.PySQLRDB.DatabaseError: <pre>Server message: Incorrect syntax near 'person'. severity(0) number(102) state(1) line(1) Server Name:ubuntu-mssql Procedure Name:</pre>
虽然我知道这是 ANSI 标准,但不幸的是,许多数据库客户端都没有很好地支持它。 PeeWee 这样做有什么具体原因吗?是否可以创建可以排除双引号的自定义数据库适配器?
如有任何帮助,我们将不胜感激。
SQL中继我不熟悉。 Peewee 引用 table 个名称(和其他实体)有几个原因:
- 它得到 peewee 开箱即用的数据库的良好支持。
- 因此您可以使用 SQL 保留字作为标识符或别名
- 如果您的 table/column 包含空格( 已经报道 信不信由你...人们会做一些奇怪的事情)。
如果不保留所有 SQL 保留字的相当全面的列表,Peewee 将不知道什么时候引号是严格要求的,什么时候是可选的。不久前在 GH 问题中讨论过这个问题。我选择不实施条件引用,因为我不想维护一个特例加上一大堆保留字。
您可以覆盖数据库上的 execute_sql() 方法-class 并使用正则表达式去除所有引号,然后再将它们发送到数据库驱动程序,但这感觉有点古怪。您还可以覆盖数据库 get_sql_context() 方法以提供您自己的 SQL 生成上下文,它也可能处理取消引用的标识符。
编辑:看起来您正在使用 sql 服务器?你可以试试转 "SET QUOTED_IDENTIFIER": https://docs.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017