1. Michael Bayer
  2. alembic

Issues

Issue #38 new

allow multiple metadata objects for autogenerate

Michael Bayer
repo owner created an issue

No description provided.

Comments (15)

  1. Michael Bayer reporter

    Currently I'm working around it roughly like this:

    our_metadata = MetaData()
    for metadata in (m1, m2, m3):
        for t in metadata.tables.values():
             if _your_check_here(t):
                t.tometadata(our_metadata)
    
  2. buddyteal7

    I have a few questions about your work around:

    Are you using this work around in env.py and setting the value of target_metadata? Or is this used somewhere separate?

    Where is MetaData() being defined?

    t.tometadata(our_metadata) - not sure how this is working or what is actually getting set.

    I really like alembic and would love to get it working with multiple models.

  3. Jon Skulski

    Well one, thanks for alembic and SQLalchemy :) Two, this issue does seem very important.

    I'm exploring options at work (essentially SQLAlchemy vs Alembic) and this issue caused me to drop Alembic. I'm glad I found the work around and would suggest that it be placed in the tutorial. I'd imagine 99% of people want to use more than one object with alembic.

    Thanks!

  4. Jon Skulski

    I'm a bit confused now. Because I tried with one of my models, and autogenerated against a blank sql database and it generated (seemingly) all the tables and columns I would need, even ones it has no relation too.

    Maybe it's the structure of the app I've inherited, and also my own confusion. Do we actually need to supply multiple metadata?

  5. Michael Bayer reporter

    it's not a feature that comes up too much. most folks seem to have a single declarative Base for their app with a MetaData and that's it.

    in your case it depends on where you are storing all your Table objects - in one MetaData or several.

  6. Jon Skulski

    Mike,

    Thank you for the reply. My comments were due to my misunderstanding. I was under the impression that you were using a per Table metadata. You can use the database metadata.

    So now I have

    from myapp import db
    target_metadata = db.metadata
    

    where I thought it had to be a specific model (target_metadata = model.metadata) and manually updated in step with code changes. Not the case!

    Hopefully, if anyone is suffering from the same delusions this will help.

    Thanks again!

  7. Dominik Czarnota

    Could you guys at least add an example into tutorial that having more then one model one can do:

    models.py file:

    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class Model1(Base):
        ...
    
    class Model2(Base):
        ...
    
    
    metadata = Base.metadata
    

    env.py file:

    from models import metadata
    target_metadata = metadata
    

    ?

  8. Log in to comment