为什么我的表无法获得相关的表信息?

时间:2019-11-19 08:27:06

标签: python peewee

我尝试使用peewee获取相关的表信息,但是它总是显示错误?

这样的代码可以正常工作:

from peewee import *
import datetime

localdb = SqliteDatabase(None)

class BaseModel(Model):
    class Meta:
        database = None
        legacy_table_names = False

def generate_tables(model):
    return 'production'

class ProductionRecord(BaseModel):
    model = CharField()
    version = CharField()
    chipid = CharField(default='')
    result = CharField()
    start_time = DateTimeField(formats=['%Y-%m-%d %H:%M:%S'])
    end_time = DateTimeField(formats=['%Y-%m-%d %H:%M:%S'])
    total_time = IntegerField()

    class Meta:
        table_function = generate_tables

class LocalProductionRecord(ProductionRecord):
    class Meta:
        database = localdb

class LocalMessageRecord(BaseModel):
    product = ForeignKeyField(ProductionRecord, backref='message')
    info = TextField()
    class Meta:
        database = localdb
        table_name = 'message'

localdb.init('bona.db')
localdb.connect(True)
# localdb.create_tables([LocalMessageRecord, LocalProductionRecord])


p1 = LocalProductionRecord.get_by_id(6)
m1 = p1.message.get()
print(m1.info)

但是在我的项目中,当我在另一个文件中获取表LocalProductionRecord和相关表信息时,它发生了一些错误,我找不到什么障碍,有人可以帮助我吗?

另一个文件代码示例:

q = LocalProductionRecord.get_by_id(pk)
print(q.model)
print(pk)
print(q.message )
print(q.message[0] )

结果如下:

C:\Users\jekoie\AppData\Local\Programs\Python\Python37-32\python.exe E:/BL800-AutoTest/main.py
803
11
SELECT `t1`.`id`, `t1`.`product_id`, `t1`.`info` FROM `message` AS `t1` WHERE (`t1`.`product_id` = 11)
Traceback (most recent call last):
  File "C:\Users\jekoie\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2947, in execute_sql
    cursor = self.cursor(commit)
  File "C:\Users\jekoie\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2933, in cursor
    self.connect()
  File "C:\Users\jekoie\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2882, in connect
    raise InterfaceError('Error, database must be initialized '
peewee.InterfaceError: Error, database must be initialized before opening a connection.

During handling of the above exception, another exception occurred:

0 个答案:

没有答案