Re: [sqlalchemy] How to create model with dynamically set schema?

2015-02-05 Thread Michael Bayer


sector119 sector...@gmail.com wrote:

 Hello!
 
 I have some model:
 
 class Person(Base):
 __tablename__ = 'person'
 
 id = Column(BigInteger, primary_key=True, autoincrement=False)
 locality_id = Column(Integer, ForeignKey(SYSTEM_SCHEMA + '.locality.id'), 
 nullable=False)
 street_id = Column(Integer, ForeignKey('street.id'), nullable=False)
 building = Column(Integer, nullable=False)
 apartment = Column(Integer, nullable=False)
 ...
 
 
 I want to create this model at different schemas: 'schema1', 'schema2', 
 'schema3' and model's FK street_id should point to 'street.id' at the same 
 schema!
 
 How can I do that?

this is an ongoing area for people, as there’s different ways to do so-called 
“multi-tenancy” vs. “sharding” etc.   So it sort of depends on what you 
actually need.  Let’s assume you need the different Person classes 
simultaneously, e.g. more of a sharding situation. 

Easiest way is to make a mixin, there’s an example at 
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName, second 
one down.

Mixins have gotten a lot better, so you should be able to do what you need.   
There’s even more fixes for mixins in 1.0 that is nearing beta releases.

-- 
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] How to create model with dynamically set schema?

2015-02-05 Thread Michael Bayer


sector119 sector...@gmail.com wrote:

 Michael, I want to set search_path to locality schema that depends on user 
 settings.

that’s more like multi-tenancy.   much easier, you don’t have to bother 
creating multiple models.

 
 And I have some kind of admin utility where user can setup locality 
 environment (schema) with set of tables and I have the list of models those 
 should be created at some schema that admin-user would specify..
 
 How can I create those list of Models at some table?

We’d like to add a “schema” feature to “execution options”, such that an 
individual set of DB operations can proceed by adding some “schema” to all 
tables or a subset of tables, but that’s a future feature.  In PG, we have 
search_path.  So you should just set up search_path as you need before you 
perform some operation.   Such as, you want to emit a create_all:


with engine.connect() as conn:
conn.execute(“set search_path = ….”)
Base.metadata.create_all(conn)

that’s all you need at that level, unless things are more complicated (like, 
you need to create tables in multiple schemas of variable names…for that I’d 
use an execution event that rewrites schema names).




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


Re: [sqlalchemy] How to create model with dynamically set schema?

2015-02-05 Thread sector119
Michael, I want to set search_path to locality schema that depends on user 
settings.

And I have some kind of admin utility where user can setup locality environment 
(schema) with set of tables and I have the list of models those should be 
created at some schema that admin-user would specify..

How can I create those list of Models at some table?

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