librelist archives

« back to archive

flask sqlalchemy setting schema

flask sqlalchemy setting schema

From:
Jeremiah Campbell
Date:
2013-03-30 @ 02:58
Hello,

Noob question - I have been reading the docs for a few hours now and have
not been successful in figuring out how to set the database schema when
using the flask-sqlalchemy extension.

Setting the schema would allow me to specify schema_name.table_name in the
underlying database.

Anyone know how to do this or have a pointer to documentation on this?

Thanks,

- Jeremiah

Re: [flask] flask sqlalchemy setting schema

From:
Nathan M
Date:
2013-03-30 @ 03:12
Hi,

do not modify directly the db, sql alxhemy will take care of it from the
models you create in python code.
You should either read the sqlalchemy documentation (see the user table
example), or look for the "flask mega tutorial". Even both:-)

Have fun!
Nathan

Hello,

Noob question - I have been reading the docs for a few hours now and have
not been successful in figuring out how to set the database schema when
using the flask-sqlalchemy extension.

Setting the schema would allow me to specify schema_name.table_name in the
underlying database.

Anyone know how to do this or have a pointer to documentation on this?

Thanks,

- Jeremiah

Re: [flask] flask sqlalchemy setting schema

From:
Jeremiah Campbell
Date:
2013-03-30 @ 03:23
Now, what if i wanted to do a slightly evil thing and have the schema be a
runtime configuration item so I can have

client1.users and
client2.users

I would only need to access one schema, not a multi-tenancy type setup
where I would need to configure the schema to be accessed before each query
or some such thing. Using sqlalchemy directly I would be able to do the
following:

meta = MetaData(schema="client1")

I am not sure how to accomplish the same thing with the flask extension.

 Again, many thanks.


On Fri, Mar 29, 2013 at 11:12 PM, Nathan M <feydaykyn@gmail.com> wrote:

> Hi,
>
> do not modify directly the db, sql alxhemy will take care of it from the
> models you create in python code.
> You should either read the sqlalchemy documentation (see the user table
> example), or look for the "flask mega tutorial". Even both:-)
>
> Have fun!
> Nathan
>
> Hello,
>
> Noob question - I have been reading the docs for a few hours now and have
> not been successful in figuring out how to set the database schema when
> using the flask-sqlalchemy extension.
>
> Setting the schema would allow me to specify schema_name.table_name in the
> underlying database.
>
> Anyone know how to do this or have a pointer to documentation on this?
>
> Thanks,
>
> - Jeremiah
>
>


-- 
---------------------------------
-- Jeremiah Campbell --
---------------------------------

Re: [flask] flask sqlalchemy setting schema

From:
Steven Kryskalla
Date:
2013-03-30 @ 03:44
On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
<jeremiah.campbell@meantheory.com> wrote:
>
> Now, what if i wanted to do a slightly evil thing and have the schema be a
> runtime configuration item so I can have
>
> client1.users and
> client2.users
>
> I would only need to access one schema, not a multi-tenancy type setup where
> I would need to configure the schema to be accessed before each query or
> some such thing. Using sqlalchemy directly I would be able to do the
> following:
>

Isn't that already configurable at run-time by changing the database
name in the connection URI? If you were using mysql:

mysql://user:pass@db/client1
vs.
mysql://user:pass@db/client2

-steve

Re: [flask] flask sqlalchemy setting schema

From:
Jeremiah Campbell
Date:
2013-03-30 @ 03:55
Target is postgres and I can't see a why to define schema in the
uri documentation for postgres.

http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#postgresql



On Fri, Mar 29, 2013 at 11:44 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
> <jeremiah.campbell@meantheory.com> wrote:
> >
> > Now, what if i wanted to do a slightly evil thing and have the schema be
> a
> > runtime configuration item so I can have
> >
> > client1.users and
> > client2.users
> >
> > I would only need to access one schema, not a multi-tenancy type setup
> where
> > I would need to configure the schema to be accessed before each query or
> > some such thing. Using sqlalchemy directly I would be able to do the
> > following:
> >
>
> Isn't that already configurable at run-time by changing the database
> name in the connection URI? If you were using mysql:
>
> mysql://user:pass@db/client1
> vs.
> mysql://user:pass@db/client2
>
> -steve
>



-- 
---------------------------------
-- Jeremiah Campbell --
---------------------------------

Re: [flask] flask sqlalchemy setting schema

From:
anh le
Date:
2013-03-30 @ 04:15
Hi,

you may try this:

class Todo(db,Model):
    __table_name__ = 'todo'
    __table_args__ = ( {'schema': 'client1'} )
    ....




On Sat, Mar 30, 2013 at 10:55 AM, Jeremiah Campbell <
jeremiah.campbell@meantheory.com> wrote:

> Target is postgres and I can't see a why to define schema in the
> uri documentation for postgres.
>
> http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#postgresql
>
>
>
> On Fri, Mar 29, 2013 at 11:44 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>
>> On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
>> <jeremiah.campbell@meantheory.com> wrote:
>> >
>> > Now, what if i wanted to do a slightly evil thing and have the schema
>> be a
>> > runtime configuration item so I can have
>> >
>> > client1.users and
>> > client2.users
>> >
>> > I would only need to access one schema, not a multi-tenancy type setup
>> where
>> > I would need to configure the schema to be accessed before each query or
>> > some such thing. Using sqlalchemy directly I would be able to do the
>> > following:
>> >
>>
>> Isn't that already configurable at run-time by changing the database
>> name in the connection URI? If you were using mysql:
>>
>> mysql://user:pass@db/client1
>> vs.
>> mysql://user:pass@db/client2
>>
>> -steve
>>
>
>
>
> --
> ---------------------------------
> -- Jeremiah Campbell --
> ---------------------------------
>

Re: [flask] flask sqlalchemy setting schema

From:
anh le
Date:
2013-03-30 @ 04:37
Sorry for the typo in my example, should be __tablename__ = 'todo'


On Sat, Mar 30, 2013 at 11:15 AM, anh le <anhlh2@gmail.com> wrote:

> Hi,
>
> you may try this:
>
> class Todo(db,Model):
>     __table_name__ = 'todo'
>     __table_args__ = ( {'schema': 'client1'} )
>     ....
>
>
>
>
> On Sat, Mar 30, 2013 at 10:55 AM, Jeremiah Campbell <
> jeremiah.campbell@meantheory.com> wrote:
>
>> Target is postgres and I can't see a why to define schema in the
>> uri documentation for postgres.
>>
>> http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#postgresql
>>
>>
>>
>> On Fri, Mar 29, 2013 at 11:44 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>>
>>> On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
>>> <jeremiah.campbell@meantheory.com> wrote:
>>> >
>>> > Now, what if i wanted to do a slightly evil thing and have the schema
>>> be a
>>> > runtime configuration item so I can have
>>> >
>>> > client1.users and
>>> > client2.users
>>> >
>>> > I would only need to access one schema, not a multi-tenancy type setup
>>> where
>>> > I would need to configure the schema to be accessed before each query
>>> or
>>> > some such thing. Using sqlalchemy directly I would be able to do the
>>> > following:
>>> >
>>>
>>> Isn't that already configurable at run-time by changing the database
>>> name in the connection URI? If you were using mysql:
>>>
>>> mysql://user:pass@db/client1
>>> vs.
>>> mysql://user:pass@db/client2
>>>
>>> -steve
>>>
>>
>>
>>
>> --
>> ---------------------------------
>> -- Jeremiah Campbell --
>> ---------------------------------
>>
>
>

Re: [flask] flask sqlalchemy setting schema

From:
Michael Herman
Date:
2013-03-30 @ 04:30
from sqlalchemy import create_engine 
engine = create_engine('postgresql///mytable.db') 
from sqlalchemy.ext.declarative import declarative_base 
Base = declarative_base() 
from sqlalchemy import Column, Integer, String 
class User(Base): # 
      Build a table/class here



On Mar 29, 2013, at 9:15 PM, anh le <anhlh2@gmail.com> wrote:

> Hi,
> 
> you may try this:
> 
> class Todo(db,Model):
>     __table_name__ = 'todo'
>     __table_args__ = ( {'schema': 'client1'} )
>     ....
> 
> 
> 
> 
> On Sat, Mar 30, 2013 at 10:55 AM, Jeremiah Campbell 
<jeremiah.campbell@meantheory.com> wrote:
>> Target is postgres and I can't see a why to define schema in the uri 
documentation for postgres.
>> 
>> http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#postgresql
>> 
>> 
>> 
>> On Fri, Mar 29, 2013 at 11:44 PM, Steven Kryskalla 
<skryskalla@gmail.com> wrote:
>>> On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
>>> <jeremiah.campbell@meantheory.com> wrote:
>>> >
>>> > Now, what if i wanted to do a slightly evil thing and have the schema be a
>>> > runtime configuration item so I can have
>>> >
>>> > client1.users and
>>> > client2.users
>>> >
>>> > I would only need to access one schema, not a multi-tenancy type setup where
>>> > I would need to configure the schema to be accessed before each query or
>>> > some such thing. Using sqlalchemy directly I would be able to do the
>>> > following:
>>> >
>>> 
>>> Isn't that already configurable at run-time by changing the database
>>> name in the connection URI? If you were using mysql:
>>> 
>>> mysql://user:pass@db/client1
>>> vs.
>>> mysql://user:pass@db/client2
>>> 
>>> -steve
>> 
>> 
>> 
>> -- 
>> ---------------------------------
>> -- Jeremiah Campbell --
>> ---------------------------------
> 

Re: [flask] flask sqlalchemy setting schema

From:
Bérenger Enselme
Date:
2013-03-30 @ 12:12
Hi,

Check SQLAlchemy author's answer on stackoverflow:
http://stackoverflow.com/a/9299021/1181200

Basically you can define the schema at metadata creation time meta =
Metadata(schema='client1')... but then you will have access to just
one schema.

Another way is to use PostgreSQL's client connection option search_path:
sess.execute("SET search_path TO client1")

Béranger


On Fri, Mar 29, 2013 at 11:55 PM, Jeremiah Campbell
<jeremiah.campbell@meantheory.com> wrote:
> Target is postgres and I can't see a why to define schema in the uri
> documentation for postgres.
>
> http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#postgresql
>
>
>
> On Fri, Mar 29, 2013 at 11:44 PM, Steven Kryskalla <skryskalla@gmail.com>
> wrote:
>>
>> On Fri, Mar 29, 2013 at 8:23 PM, Jeremiah Campbell
>> <jeremiah.campbell@meantheory.com> wrote:
>> >
>> > Now, what if i wanted to do a slightly evil thing and have the schema be
>> > a
>> > runtime configuration item so I can have
>> >
>> > client1.users and
>> > client2.users
>> >
>> > I would only need to access one schema, not a multi-tenancy type setup
>> > where
>> > I would need to configure the schema to be accessed before each query or
>> > some such thing. Using sqlalchemy directly I would be able to do the
>> > following:
>> >
>>
>> Isn't that already configurable at run-time by changing the database
>> name in the connection URI? If you were using mysql:
>>
>> mysql://user:pass@db/client1
>> vs.
>> mysql://user:pass@db/client2
>>
>> -steve
>
>
>
>
> --
> ---------------------------------
> -- Jeremiah Campbell --
> ---------------------------------