1

I'm starting a project with SQLAlchemy and Alembic. I'm using SQLite for testing purposes.

When I execute alembic revision --autogenerate -m "..." it correctly generates the migrations and when I execute alembic upgrade head it creates the table and run all migrations as expected. I can access, read and write directly into the DB with an extension.

The overall structure is:

root
   |-- alembic
   |-- engine
      |-- models
         |-- __init__.py
         |-- some_model.py
         ...
      |-- db_interface.py
      |-- settings.py
      |-- test.db
      ...

Inside engine/models/__init__.py :

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session

from settings import DBURI
from .some_model import SomeModel

engine = create_engine(DBURI)
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)

The value of DBURI is just 'sqlite:///test2.db', and it must be correct, as alembic also import the same value.

However this code fails (both from python interface or executing it inside db_interface.py)

from models import Session, SomeModel
db_session = Session()
db_session.query(SomeModel).first()

Piece of traceback:

File "/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1277, in _execute_context
    cursor, statement, parameters, context
  File "/venv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: exchanges

I've also tried using this line: db_session.query('some_model').first() ('some_model' is the tablename) The traceback changes as if it were looking for a column, instead of a table:

sqlite3.OperationalError: no such column: some_model

Can anyone point out what I'm doing wrong?

EDIT:

Also, if I execute this:

from models import engine
engine.table_names()

It returns an empty array.

Justcurious
  • 1,952
  • 4
  • 11
  • 17
  • 2
    `'sqlite:///test2.db'` is a relative url. Are you sure alembic isn't updating a _different_ `test2.db`? – snakecharmerb Dec 19 '20 at 19:26
  • 2
    Just deleted the `test2.db` and executed the migrations again to be sure. Alembic is creating/updating the correct file. – Justcurious Dec 19 '20 at 19:28
  • 1
    What if you specify an [absolute path](https://docs.sqlalchemy.org/en/13/core/engines.html#sqlite) for both the app and alembic? – snakecharmerb Dec 19 '20 at 19:45
  • 1
    **Ok, so you were correct the first time.** I did deleted the file and executed migrations, as I said, but I also executed a script that tried to access the DB, so when it didn't found the `test2.db` it created an empty one where I had previously deleted. Therefore I assumed it was done by alembic. In fact alembic was creating the db file in one place and the script creating it in another. Sorry about that and thank you very much. If you want to write it as an answer I would be happy to accept it. – Justcurious Dec 19 '20 at 20:09
  • The accepted answer to the linked duplicate is probably as good as any answer I could write. – snakecharmerb Jan 03 '21 at 11:38

0 Answers0