Search code examples
pythonpython-3.xpeewee

Cannot initialize peewee's in-memory databse


I'm trying to set in-memory database for testing with peewee. However, I cannot initialize database:

>>> import peewee
>>> import Entities
>>> db = peewee.SqliteDatabase(':memory:')
>>> db.connect()
True

This above is okay. Then I got following error:

>>> 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.

Me entities are all right for sure, since I use peewee for managing data in sqlite database without troubles. They came when I made a try to create tests with in-memory database. The same error happens when I try to create tables:

>>> 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.

I'm using:

  • Python 3.7.1
  • Peewee 3.9.2

Anybody faced similar problem?


Solution

  • I found the reason of the error. It looks I forgot to put foreign_key pragma:

    >>> 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)
    

    When I put pragmas={'foreign_keys': 1}, which is what my entity requires, this error disappears.