Luis,

This solution seems like it would work fine. There are many different ways
you can handle database multi-tenancy and it really all depends on how you
want to partition your client data.

Recently, I had a project that required a multi-tenant configuration. I
decided to use Postgres' Schema functionality (
http://www.postgresql.org/docs/9.3/static/sql-createschema.html) for
handling multiple tenants that required the same table structure. I'm not
sure if this is an option for you at this point in your project but I
recommend you explore some of the multi-tenancy options your database may
provide.

-Vincent




On Wed, May 21, 2014 at 8:05 AM, Luis Aguirre <[email protected]> wrote:

> Hi guys,
>
> I'm developing a multi-tenant application.  The model is pretty simple:
>
> * Each user as a tenant id
> * Some objects has a tenant_id and some doesn't, if the object has a
> tenant_id, I have to filter by this colum
>
> I don't want to have a
> DBSession.query(SomeObject).filter(filter_by_tenant_id).filter(some_other_filter).all()
>
> So I had a method to DBSession called tdb that do all the work and return
> a clear Query Object, so I did the following:
>
> #in application __init__
> config.add_subscriber(tenantdb_on_new_request, events.NewRequest)
>
> #In models
> DBSession =
> scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
>
>
> def tenantdb_on_new_request(event):
>
>     def tenant_db(*entities, **kwargs):
>         q = DBSession.query(*entities, **kwargs)
>         for e in entities:
>             if hasattr(e, 'tenant_id'):
>                 q = q.filter(e.tenant_id == event.request.user.tenant)
>         return q
>
>     DBSession.tdb = tenant_db
>
> It's works fine, but it's ok? May I have problems when many requests came
> in production?  Some idea to do it in other way?
>
> Thanks in advance!
>
> Best regards,
>
> Luis Aguirre
>
>  --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Vincent Catalano
Software Engineer and Web Developer,
(520).603.8944

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

Reply via email to