librelist archives

« back to archive

Removing "db" prefix in flask-sqlalchemy

Removing "db" prefix in flask-sqlalchemy

From:
Wael Orabi
Date:
2012-06-08 @ 00:04
hello there,

I'm not sure if anyone brought this up before but is there a way to
directly import the classes that are part of SQLAlchemy object of
flask-sqlalchemy?

anything better than "(Model, Column, String, Integer, DateTime) =
(db.Model, db.Column, db.String, db.Integer, db.DateTime)" would be great.

nothing urgent though, it's only for decorative purposes.

(sorry if you got this twice, used the wrong email address before)

cheers
--w

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Paul Gibbs
Date:
2012-06-08 @ 00:16
I extend my model classes from db.Model, and then import all other 
SQLAlchemy objects directly from SQLalchemy – so far it's worked for me.

–Paul  


On Thursday, June 7, 2012 at 5:04 PM, Wael Orabi wrote:

> hello there,
>  
> I'm not sure if anyone brought this up before but is there a way to 
directly import the classes that are part of SQLAlchemy object of 
flask-sqlalchemy?  
>  
> anything better than "(Model, Column, String, Integer, DateTime) = 
(db.Model, db.Column, db.String, db.Integer, db.DateTime)" would be great.
>  
> nothing urgent though, it's only for decorative purposes.
>  
> (sorry if you got this twice, used the wrong email address before)
>  
> cheers
> --w
>  

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Juan Pablo Scaletti
Date:
2012-06-11 @ 14:32
On 07/06/2012, at 19:04, Wael Orabi wrote:

> hello there,
> 
> I'm not sure if anyone brought this up before but is there a way to 
directly import the classes that are part of SQLAlchemy object of 
flask-sqlalchemy?

Only Model is part of the SQLAlchemy object, the rest in alisased from 
SQLAlchemy for your convinience,
so you don't have to write these import in every models file:

from sqlalchemy import *
from sqlalchemy.orm import *

> 
> anything better than "(Model, Column, String, Integer, DateTime) = 
(db.Model, db.Column, db.String, db.Integer, db.DateTime)" would be great.
> 
> nothing urgent though, it's only for decorative purposes.
> 
> (sorry if you got this twice, used the wrong email address before)
> 
> cheers
> --w
> 

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Luca Lesinigo
Date:
2012-06-21 @ 16:02
Il giorno 11/giu/2012, alle ore 16:32, Juan Pablo Scaletti ha scritto:
> Only Model is part of the SQLAlchemy object, the rest in alisased from 
SQLAlchemy for your convinience, so you don't have to write these import 
in every models file:
> from sqlalchemy import *
> from sqlalchemy.orm import *

FYI, doing that I cannot make the Many-to-Many example to work because the
Table is expecting the metadata as second argument.

I have to either get metadata from Flask-SQLAlchemy's object and pass it 
to SQLAlchemy's Table:
  from myapp import db
  (Model, metadata) = (db.Model, db.metadata)
  from sqlalchemy import Table
  # ...
  _foobar = Table('foobar', metadata, Column(...), Column(...))

or I get the Table from Flask-SQLAlchemy's object:
  from myapp import db
  (Model, Table) = (db.Model, db.Table)
  _foobar = Table('foobar', Column(...), Column(...))

--
Luca Lesinigo

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Juan Pablo Scaletti
Date:
2012-06-21 @ 16:19
?  Of course you have to.
If you really despise seeing the "db." in your model declarations, even 
for those cases, make an intermediate file and import from there.

On 21/06/2012, at 11:02 a.m., Luca Lesinigo <luca@lesinigo.it> wrote:

> Il giorno 11/giu/2012, alle ore 16:32, Juan Pablo Scaletti ha scritto:
>> Only Model is part of the SQLAlchemy object, the rest in alisased from 
SQLAlchemy for your convinience, so you don't have to write these import 
in every models file:
>> from sqlalchemy import *
>> from sqlalchemy.orm import *
> 
> FYI, doing that I cannot make the Many-to-Many example to work because 
the Table is expecting the metadata as second argument.
> 
> I have to either get metadata from Flask-SQLAlchemy's object and pass it
to SQLAlchemy's Table:
>  from myapp import db
>  (Model, metadata) = (db.Model, db.metadata)
>  from sqlalchemy import Table
>  # ...
>  _foobar = Table('foobar', metadata, Column(...), Column(...))
> 
> or I get the Table from Flask-SQLAlchemy's object:
>  from myapp import db
>  (Model, Table) = (db.Model, db.Table)
>  _foobar = Table('foobar', Column(...), Column(...))
> 
> --
> Luca Lesinigo
> 

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Luca Lesinigo
Date:
2012-06-21 @ 18:09
Il giorno 21/giu/2012, alle ore 18:19, Juan Pablo Scaletti ha scritto:
> ?  Of course you have to.
> If you really despise seeing the "db." in your model declarations, even 
for those cases, make an intermediate file and import from there.
Yeah, I know that. I just stumbled upon this thread while researching for 
my other question[*].

I then noted that you wrote to Wael:
> Only Model is part of the SQLAlchemy object, the rest in alisased from 
SQLAlchemy for your convinience
and I just wanted to point out that Table doesn't seem to be just "aliased
from SQLAlchemy", since SQLAlchemy's Table wants me to specify metadata 
while Flask-SQLAlchemy's one seems to get along nicely even without it: 
there must be some subtle difference between the two.

But I confess I just don't grasp all the details enough to know _why_ it 
is that way :)

[*] 
http://librelist.com/browser//flask/2012/6/21/sharing-a-model-between-flask-sqlalchemy-and-other-apps/

--
Luca Lesinigo

Re: [flask] Removing "db" prefix in flask-sqlalchemy

From:
Simon Sapin
Date:
2012-06-21 @ 18:16
Le 21/06/2012 20:09, Luca Lesinigo a écrit :
> But I confess I just don't grasp all the details enough to know_why_  it
is that way

I don’t know the details here, but a general goal of Flask-SQLAlchemy is 
to require less explicitly from you, because it can find stuff in 
Flask’s request context.

You can test if two objects are really the same (only imported into 
another namespace for convenience) with the 'is' operator.
If `a.Foo is b.Foo` is false, there is some difference and you should 
check the docs.

Regards,
-- 
Simon Sapin