Peewee ORM 中 select 的子字符串
Substring from select in Peewee ORM
我正在将我的程序的代码库从 Peewee v2 更新到 v3。
在代码中,我在 select 语句中执行了一个子字符串,该语句在 v2 中始终有效,但现在不再有效。
我有以下 table
class Project(BaseModel):
project_id = CharField(column_name='projectID', primary_key=True)
name = CharField()
relative_path = CharField()
product_id = ForeignKeyField(column_name='productID', model=Product, field='product_id')
class Meta:
table_name = 'project'
indexes = (
(('name', 'product_id'), True),
)
我的查询如下:
project_prefix = "INH"
query = internal_projects = Project.select(fn.substring(Project.project_id, len(project_prefix) + 1))\
.where(Project.project_id.startswith(project_prefix))
for q in query:
print q
这给了我结果:
None
None
None
None
None
None
None
但是,如果我将 fn.substring 排除在查询之外,结果就像:
INH00001
INH00002
INH00004
INH00005
INH00006
INH00007
INH00008
我从第一个查询中得到 "None" 的次数确实与第二个查询的结果数相匹配,所以它肯定是 selecting 东西。我怎样才能使我的第一个查询再次工作,以便得到预期的结果?例如:
00001
00002
00004
00005
00006
00007
00008
我猜您访问属性的方式有些古怪。尝试明确指定别名:
pid = fn.substring(Project.project_id, len(prefix + 1))
query = (Project
.select(pid.alias('pid'))
.where(Project.project_id.startswith(prefix)))
for project in query:
print(project.pid)
我正在将我的程序的代码库从 Peewee v2 更新到 v3。 在代码中,我在 select 语句中执行了一个子字符串,该语句在 v2 中始终有效,但现在不再有效。 我有以下 table
class Project(BaseModel):
project_id = CharField(column_name='projectID', primary_key=True)
name = CharField()
relative_path = CharField()
product_id = ForeignKeyField(column_name='productID', model=Product, field='product_id')
class Meta:
table_name = 'project'
indexes = (
(('name', 'product_id'), True),
)
我的查询如下:
project_prefix = "INH"
query = internal_projects = Project.select(fn.substring(Project.project_id, len(project_prefix) + 1))\
.where(Project.project_id.startswith(project_prefix))
for q in query:
print q
这给了我结果:
None
None
None
None
None
None
None
但是,如果我将 fn.substring 排除在查询之外,结果就像:
INH00001
INH00002
INH00004
INH00005
INH00006
INH00007
INH00008
我从第一个查询中得到 "None" 的次数确实与第二个查询的结果数相匹配,所以它肯定是 selecting 东西。我怎样才能使我的第一个查询再次工作,以便得到预期的结果?例如:
00001
00002
00004
00005
00006
00007
00008
我猜您访问属性的方式有些古怪。尝试明确指定别名:
pid = fn.substring(Project.project_id, len(prefix + 1))
query = (Project
.select(pid.alias('pid'))
.where(Project.project_id.startswith(prefix)))
for project in query:
print(project.pid)