简体   繁体   English

peewee在select语句中未使用动态表名

[英]peewee not using dynamic table name in select statement

Why does the select statement here have t1 instead of MyDynamicTable ? 为什么此处的select语句具有t1而不是MyDynamicTable

from peewee import *

database = SqliteDatabase(None)


class Base(Model):
    class Meta:
        database = database


class MyTable(Base):
    FieldA = TextField()
    FieldB = TextField()


mytable = type('MyDynamicTable', (MyTable,), {})

database.init('test.db')

mytable.select()

Leads to: 导致:

>>> mytable.select()
<class 'peewee.MyDynamicTable'> SELECT "t1"."id", "t1"."FieldA", "t1"."FieldB" FROM "mydynamictable" AS t1 []

But the name is correct: 但是这个名字是正确的:

>>> mytable
<class 'peewee.MyDynamicTable'>
>>> mytable._meta.db_table
'mydynamictable'

Peewee has aliased your table name. Peewee为您的表名加上了别名。 If you read the full query: 如果您阅读完整的查询:

SELECT "t1"."id", "t1"."FieldA", "t1"."FieldB"
FROM "mydynamictable" AS t1

The "mydynamictable" AS t1 part aliases the table name to "t1", to make the query more compact. “ mydynamictable” AS t1部分将表名别名为“ t1”,以使查询更紧凑。 This is especially important when you have joins and need to disambiguate columns with the same name. 当您有联接并且需要消除具有相同名称的列的歧义时,这一点尤其重要。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM