librelist archives

« back to archive

sqlalchemy table class, inherit from base

sqlalchemy table class, inherit from base

From:
Cornelius Kölbel
Date:
2014-11-26 @ 14:37
Hi,

I am using flask-sqlalchemy in my project.
I have several table class definition, that have similar methods.

class Realm(db.Model)
    __tablename__ = realm

    ...

    def delete(self):
        pass
        # do some tidy up...


As many of those clases have some methods, I thought why not define a
base class.
But when doing this, python complains about that the base class has no
table name, and no primary key and so on.
How could I achieve this?
Does this make sense at all?

THanks a lot and kind regards
Cornelius

Re: [flask] sqlalchemy table class, inherit from base

From:
Jon Ribbens
Date:
2014-11-26 @ 15:24
On Wed, Nov 26, 2014 at 03:37:07PM +0100, Cornelius Kölbel wrote:
> I am using flask-sqlalchemy in my project.
> I have several table class definition, that have similar methods.
> 
> class Realm(db.Model)
>     __tablename__ = realm
> 
>     ...
> 
>     def delete(self):
>         pass
>         # do some tidy up...
> 
> 
> As many of those clases have some methods, I thought why not define a
> base class.
> But when doing this, python complains about that the base class has no
> table name, and no primary key and so on.
> How could I achieve this?
> Does this make sense at all?

My total guess is that your base class is inheriting from SQLAlchemy's
stuff, and its magic is getting unhappy. I would suggest, rather than
making your class a base class, make it a mix-in instead, i.e.:

  class MyTableMethods:
    def foo(self):
      ... blah ...

  class MyTable(db.Model, MyTableMethods):
    __tablename__ = "foo"

Re: [flask] sqlalchemy table class, inherit from base

From:
Matthias Urlichs
Date:
2014-11-26 @ 15:42
Hi,

Cornelius Kölbel:
> But when doing this, python complains about that the base class has no
> table name, and no primary key and so on.
> How could I achieve this?

http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/declarative.html#abstract
-- 
-- Matthias Urlichs

Re: [flask] sqlalchemy table class, inherit from base

From:
David Baumgold
Date:
2014-11-26 @ 15:26
You probably want to use a SQLAlchemy 
Mixin: http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/declarative.html#mixin-and-custom-base-classes Basically,
for the class that defines the functionality you want to share, inherit 
from “object” rather than “db.Model”.

David Baumgold


From: Cornelius Kölbel <cornelius@privacyidea.org>
Reply: flask@librelist.com <flask@librelist.com>>
Date: November 26, 2014 at 9:39:25 AM
To: flask@librelist.com <flask@librelist.com>>
Subject:  [flask] sqlalchemy table class, inherit from base  

Hi,  

I am using flask-sqlalchemy in my project.  
I have several table class definition, that have similar methods.  

class Realm(db.Model)  
__tablename__ = realm  

...  

def delete(self):  
pass  
# do some tidy up...  


As many of those clases have some methods, I thought why not define a  
base class.  
But when doing this, python complains about that the base class has no  
table name, and no primary key and so on.  
How could I achieve this?  
Does this make sense at all?  

THanks a lot and kind regards  
Cornelius  

Re: [flask] sqlalchemy table class, inherit from base

From:
Cornelius Kölbel
Date:
2014-11-26 @ 15:43
Hi Jon, Hi David,

honestly I haven't worked with Mixin before, but that was exactly what I
needed.
Tests still run fine :-)
THanks a lot
Cornelius

Am 26.11.2014 um 16:26 schrieb David Baumgold:
> You probably want to use a SQLAlchemy
> Mixin: 
http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/declarative.html#mixin-and-custom-base-classes
Basically,
> for the class that defines the functionality you want to share,
> inherit from “object” rather than “db.Model”.
>
> David Baumgold
>
>
> From: Cornelius Kölbel <cornelius@privacyidea.org>
> <mailto:c%0A%20ornelius@privacyidea.org>
> Reply: flask@librelist.com <flask@librelist.com>>
> <mailto:flask@librelist.com>
> Date: November 26, 2014 at 9:39:25 AM
> To: flask@librelist.com <flask@librelist.com>>
> <mailto:flask@librelist.com>
> Subject: [flask] sqlalchemy table class, inherit from base
>
>> Hi,
>>
>> I am using flask-sqlalchemy in my project.
>> I have several table class definition, that have similar methods.
>>
>> class Realm(db.Model)
>> __tablename__ = realm
>>
>> ...
>>
>> def delete(self):
>> pass
>> # do some tidy up...
>>
>>
>> As many of those clases have some methods, I thought why not define a
>> base class.
>> But when doing this, python complains about that the base class has no
>> table name, and no primary key and so on.
>> How could I achieve this?
>> Does this make sense at all?
>>
>> THanks a lot and kind regards
>> Cornelius