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.