Re: [sqlalchemy] postgres schemas at runtime

2015-03-12 Thread Francesco Della Vedova
I thought about wrapping create_all this way:

def create_tables(database, schema=None):
for table in Base.metadata.tables.values():
table.schema = schema
Base.metadata.create_all(database.engine)

and then probably use the event for all the queries afterwards. Is this too 
much of a nasty way of doing it?

On Thursday, March 12, 2015 at 6:29:00 PM UTC+1, Michael Bayer wrote:
>
>
>
> Francesco Della Vedova > 
> wrote: 
>
> > Hello, 
> > 
> > I have a set of models that I would like to replicate on different 
> Postgres schemas. The name of the schema is only known at runtime. 
> > Just as an example, imagine I have a bunch of tables of customer data, 
> and I want to create a schema 'customer_id' with the same tables every time 
> I get a new customer. 
> > 
> > I'm aware of this strategy but it wouldn't work in my case. 
> > 
> > class TableA(Base): 
> >... 
> > 
> > class TableB(Base): 
> >... 
> > 
> > for customer_id in customers: 
> ># set the schema here 
> >Base.metadata.create_all() 
>
> you can try using table.tometadata(new_metadata, schema=) to 
> make 
> new tables, and possibly even use it in conjunction with automap 
> (http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html). 
>
> Another approach is to use a before_cursor_execute() event, searching for 
> a 
>  token in the SQL and rewriting it to one that is set up on the 
> current connection in the .info dictionary. 
>
> The latter is essentially a hacky way to get at an upcoming feature which 
> is 
>
> https://bitbucket.org/zzzeek/sqlalchemy/issue/2685/default-schema-schema-translation-map-as.
>  
>
>
> This depends of course on if you need to use each schema on a 
> per-Session/connection basis, or you need to work with all schema-based 
> models simultaneously. 
>
>
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to sqlalchemy+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at http://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] postgres schemas at runtime

2015-03-12 Thread Michael Bayer


Francesco Della Vedova  wrote:

> Hello, 
> 
> I have a set of models that I would like to replicate on different Postgres 
> schemas. The name of the schema is only known at runtime. 
> Just as an example, imagine I have a bunch of tables of customer data, and I 
> want to create a schema 'customer_id' with the same tables every time I get a 
> new customer.
> 
> I'm aware of this strategy but it wouldn't work in my case. 
> 
> class TableA(Base):
>...
> 
> class TableB(Base):
>...
> 
> for customer_id in customers:
># set the schema here
>Base.metadata.create_all()

you can try using table.tometadata(new_metadata, schema=) to make
new tables, and possibly even use it in conjunction with automap
(http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html).

Another approach is to use a before_cursor_execute() event, searching for a
 token in the SQL and rewriting it to one that is set up on the
current connection in the .info dictionary.

The latter is essentially a hacky way to get at an upcoming feature which is
https://bitbucket.org/zzzeek/sqlalchemy/issue/2685/default-schema-schema-translation-map-as.

This depends of course on if you need to use each schema on a
per-Session/connection basis, or you need to work with all schema-based
models simultaneously.


> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.