Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple metadata objects for autogenerate #38

Closed
sqlalchemy-bot opened this issue Mar 26, 2012 · 18 comments
Closed

allow multiple metadata objects for autogenerate #38

sqlalchemy-bot opened this issue Mar 26, 2012 · 18 comments

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Michael Bayer (@zzzeek)

@sqlalchemy-bot
Copy link
Author

buddyteal7 wrote:

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.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

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)

@sqlalchemy-bot
Copy link
Author

buddyteal7 wrote:

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.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

this is in env.py. MetaData is defined within the application's model, then the local "our_metadata" is local to env.py. tometadata() is here: http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html?highlight=tometadata#sqlalchemy.schema.Table.tometadata .

@sqlalchemy-bot
Copy link
Author

buddyteal7 wrote:

Got it. Thanks for taking the time to clarify, much appreciated.

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • added labels: autogenerate - detection

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • set milestone to "tier 1"

@sqlalchemy-bot
Copy link
Author

Jon Skulski (@jskulski) wrote:

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!

@sqlalchemy-bot
Copy link
Author

Jon Skulski (@jskulski) wrote:

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?

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

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.

@sqlalchemy-bot
Copy link
Author

Jon Skulski (@jskulski) wrote:

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!

@sqlalchemy-bot
Copy link
Author

Dominik Czarnota (@disconnect3d) wrote:

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

?

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

@czarnot the models.py you illustrate there has just one MetaData object. http://alembic.readthedocs.org/en/latest/autogenerate.html illustrates this.

@sqlalchemy-bot
Copy link
Author

Pankaj Lal wrote:

Any chance, that this is going to be fixed any time soon.

See my question here.

http://stackoverflow.com/questions/38878887/handling-multiple-models-py-with-alembic

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

@prlal I dont know the answer to that question. the default logging goes to stdout and should illustrate every table its seeing. I'd check that these tables don't already exist in the database.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

https://gerrit.sqlalchemy.org/318

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

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

7d5f6ea

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant