我正在尝试设置内存数据库以使用peewee进行测试。但是,我无法初始化数据库:
>>> import peewee
>>> import Entities
>>> db = peewee.SqliteDatabase(':memory:')
>>> db.connect()
True
这没关系。然后出现以下错误:
>>> db.bind(Entities.RouterSettings)
Traceback (most recent call last):
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2833, in execute_sql
cursor = self.cursor(commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2819, in cursor
self.connect()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2768, 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:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 3008, in bind
for model in models:
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 5630, in __iter__
return iter(self.select())
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 6198, in __iter__
self.execute()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 1698, in inner
return method(self, database, *args, **kwargs)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 1769, in execute
return self._execute(database)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 1943, in _execute
cursor = database.execute(self)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2848, in execute
return self.execute_sql(sql, params, commit=commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2842, in execute_sql
self.commit()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2627, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 178, in reraise
raise value.with_traceback(tb)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2833, in execute_sql
cursor = self.cursor(commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2819, in cursor
self.connect()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2768, in connect
raise InterfaceError('Error, database must be initialized '
peewee.InterfaceError: Error, database must be initialized before opening a connection.
我的实体肯定可以,因为我使用peewee来管理sqlite数据库中的数据而没有麻烦。当我尝试使用内存数据库创建测试时,它们就来了。当我尝试创建表时也会发生相同的错误:
>>> db = peewee.SqliteDatabase(':memory:')
>>> db.connect()
True
>>> db.create_tables([Entities.RouterSettings], safe=True)
Traceback (most recent call last):
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2833, in execute_sql
cursor = self.cursor(commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2819, in cursor
self.connect()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2768, 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:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2995, in create_tables
model.create_table(**options)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 6012, in create_table
cls._schema.create_all(safe, **options)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 5225, in create_all
self.create_table(safe, **table_options)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 5111, in create_table
self.database.execute(self._create_table(safe=safe, **options))
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2848, in execute
return self.execute_sql(sql, params, commit=commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2842, in execute_sql
self.commit()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2627, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 178, in reraise
raise value.with_traceback(tb)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2833, in execute_sql
cursor = self.cursor(commit)
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2819, in cursor
self.connect()
File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2768, in connect
raise InterfaceError('Error, database must be initialized '
peewee.InterfaceError: Error, database must be initialized before opening a connection.
我正在使用:
有人遇到类似的问题吗?
答案 0 :(得分:0)
我找到了错误的原因。我似乎忘记了foreign_key
编译指示:
>>> import peewee
>>> test_db = peewee.SqliteDatabase(':memory:', pragmas={'foreign_keys': 1})
import Entities
>>> models = [Entities.RouterSettings]
>>> test_db.bind(models, bind_refs=False, bind_backrefs=False)
>>> test_db.connect()
True
>>> test_db.create_tables(models)
当我放置实体所需的pragmas={'foreign_keys': 1}
时,此错误消失了。