Issue #38 new

allow multiple metadata objects for autogenerate

Mike Bayer
repo owner created an issue

No description provided.

Comments (13)

  1. buddyteal7

    It is very common to have multiple models and many tables. This feature seems quite important. I'm surprised more people haven't run in to this issue.

  2. Mike 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):
  3. buddyteal7

    I have a few questions about your work around:

    Are you using this work around in 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.

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


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

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

  7. Jon Skulski


    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!

  8. Dominik C

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

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

    from models import metadata
    target_metadata = metadata


  9. Log in to comment