allow multiple metadata objects for autogenerate

Issue #38 resolved
Michael Bayer
repo owner created an issue

No description provided.

Comments (17)

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

  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.


  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


    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: 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


  8. Michael Bayer reporter

    Allow target_metadata to be a sequence

    The :paramref:.EnvironmentContext.configure.target_metadata parameter may now be optionally specified as a sequence of :class:.MetaData objects instead of a single :class:.MetaData object. The autogenerate process will process the sequence of :class:.MetaData objects in order.

    Change-Id: I6485c05d68219ff7af1611b34550487d316e0242 Fixes: #38

    → <<cset 7d5f6eabeccb>>

  9. Log in to comment