Hi, I wanted to post here since I had to hack around an issue in order to
use SQLAlchemy with a dynamic Postgres schema, and I wanted to see if
there's a way I can get the workarounds out of my code.
In my app I'm separating the data by dynamically managing the schemas. The
basic way this is achieved is:
new_meta = MetaData(schema='newschema')
for table in old_meta.tables.items():
table.tometadata(new_meta)
new_meta.create_all()
This worked nicely until I tried to use postgresql.ENUM type. The ENUM is a
first class schema object so it needs to be created separately, but it does
this by registering an event on the first table it's bound to (doesnt
transfer to copies) and it doesn't inherit the schema of the table it's
bound to (in fact, I'm also working around this behaviour in order to apply
DDL's to each schema).
Potentially this is correct but in my case I don't want dependencies
between schemas so I had to subclass Column and postgresql.ENUM to copy the
type and assign the schema at the correct time (code below).
I'd very much like to not have these hacks/workarounds in my program, is
there a cleaner way to do this or would the developers be open to a patch?
I would suggest an argument to the ENUM type to indicate that it inherits
the schema from the table, or perhaps allowing the registered events to be
transferred somehow.
Code for the workaround I implemented:
class Column(sqlalchemy.Column):
def _constructor(self, *args, **kwargs):
# Copy type so we can mutate it safely without impacting other
schemas
kwargs['type_'] = kwargs['type_'].adapt(type(kwargs['type_']))
return super(Column, self)._constructor(*args, **kwargs)
class ENUM(postgresql.ENUM):
def _set_table(self, column, table):
# Inherit the schema of the table
self.schema = table.schema
return super(ENUM, self)._set_table(column, table)
If we can agree on something I'm willing to write a patch.
Regards
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.