-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extend create_all / drop_all to include schemas #3914
Comments
Changes by Omni Flux (@omniflux):
|
Michael Bayer (@zzzeek) wrote: There's ways to get this in as extension points on your own end. Unfortunately the concept of a "schema" is not very portable across databases. In MySQL, there's no "CREATE SCHEMA"; there's just other databases, e.g. "CREATE DATABASE" which the user account will often not have access to do, plus there's a lot of other arguments that go along with creating a database. In SQLite, there's no "CREATE SCHEMA", there's separate files you can attach to. In Oracle, there's no "CREATE SCHEMA", there are other user accounts that act as a namespace which again have very different permissions/syntax, additionally there are synonyms that can refer to other kinds of objects like remote tablespaces and such. Only Postgresql and SQL Server link the "schema" to a name that corresponds (usually) to "CREATE SCHEMA", and even then, those names might be symbolic names to something else, since you can put dotted symbols and other expressions into "schema". The easy way to throw in "CREATE SCHEMA" with your metadata is just a simple event, and this is acceptable to add to the docs / examples:
The DDL code above is directly from the documentation at http://docs.sqlalchemy.org/en/latest/core/ddl.html#custom-ddl and we can add this example too. Since this is very specific to Postgresql / SQL Server and is also quite simple I'm not sure there's value in making more of a pattern out of it than that. |
Omni Flux (@omniflux) wrote: Is it possible to access %(schema)s in the event.listen for metadata so foo would not have to be hardcoded? |
Michael Bayer (@zzzeek) wrote: sure you can iterate through metadata.tables and pull it:
|
Omni Flux (@omniflux) wrote: This is exactly what I was looking for, thank you. |
Michael Bayer (@zzzeek) wrote: this could use some more documentation examples but for the time being a "Schema" is kind of too much of a moving target to be worth a first class construct. |
Changes by Michael Bayer (@zzzeek):
|
Diego Quintana wrote: @zzzeek Sorry to revive such an old topic with my elementary questions . I understand the
|
Michael Bayer (@zzzeek) wrote:
The DDL() construct is mostly a short cut to invoking a single SQL statement given an event to listen for, and it also gives you the "execute_if" thing which we don't need here. We need to run multiple SQL statements within the one event using a loop, as well as figure out what schemas we will be running for, so we fill out the body of the event with a full function rather than using the DDL() shorthand-syntax which does not give us those options.
I'm not sure what that's asking. this issue is about how to emit CREATE SCHEMA. If you want to create tables given a declarative base, usually we just say Base.metadata.create_all(engine). You can run on individual tables using |
Migrated issue, originally created by Omni Flux (@omniflux)
It would be nice if Metadata.create_all and drop_all would also automatically create and drop schemas as necessary.
The text was updated successfully, but these errors were encountered: