aio-libs / aiohttp-devtools

dev tools for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reset-database don't create tables

Skorpyon opened this issue · comments

I try example app based on template.

make reset-database don't create tables.

>>> Base.metadata
MetaData(bind=None)
>>> Base.metadata.tables
immutabledict({})

Hi I see you fixed this, did you get it working?

Yes, it is my mistake plus sqlalchemy magic.

I tried split models.py to models/user.py, models/company.py
So i made models_utils.py, declare Base in this file, import this Base to all models subfiles and to management.py.
And, sure, as result I import to management uninitialized instance of Base with empty tables dict.

Unfortunately, I don't see right way now how use this recipe. I need write all 50 models in one giant models.py or split it and made UsersBase, CompanyBase, CallsBase, import all this declatarion wrappers to management and create tables one by one.

I think this approach should work, the reason you're having trouble is that you're not loading the other files which declare the models before creating the db. This should work:

models/common.py:

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

models/user.py:

form .common import Base

class User(Base):
    ...

management.py

from .models.common import Base
# Doing this forces python to execute user.py and therefore "create" that model:
from .models.user import User  # noqa
...

Yeap, it is brilliance. User didn't executed and I import "empty" Base.
Thank you very very much!

no problem, glad it help. Let me know if you have any more feedback on aiohttp-devtools.

May you suggest some way how create migrations workflow?

Let me know if you have any more feedback on aiohttp-devtools.

You not only help with tooling aiohttp but open me type hinting in python too )))

humm, there's no elegant way of doing migrations I know of. There's alembic but it's not that attractive.