an abstract base class isn't going to set up the same fields on all
descendants since the mapper()/Table() setup occurs during the
creation of the individual class using the non-inherited class dict,
and unique instances of each of the Column, etc. elements are required
as well.
"concrete inheritance", as referenced in that post, was not designed
to be used as a configurational spacesaver and always requires a
mapped selectable for the "base", which you don't have here, so it's
not appropriate for this use case.
So for this you'd need a custom metaclass:
class MyMeta(DeclarativeMeta):
def __init__(cls, classname, bases, dict_):
cls.notes = Column(String)
DeclarativeMeta.__init__(cls, classname, bases, dict_)
Base = declarative_base(metaclass=MyMeta)
On Dec 16, 2008, at 12:36 PM, Joril wrote:
>
> Hi everyone!
> I need to declare a few unrelated classes (from a "business"
> perspective) that share some attributes/fields, so I thought I could
> use an abstract class to group these common attributes (note that just
> a few classes should have them, not every one)
>
> This post
> http://groups.google.it/group/sqlalchemy/msg/d3de02f609a0bbd9?hl=it
> suggests to use mixins, but I can't get it to work, SQLA generates the
> tables without the common fields.
>
> I'm testing it with this script:
>
>> from sqlalchemy.ext.declarative import declarative_base
>> from sqlalchemy import Column, Integer, String
>> from sqlalchemy import create_engine
>>
>> # --- Tables definition
>> Base = declarative_base()
>>
>> class Abstract(object):
>> notes = Column(String)
>>
>> class Concrete(Base, Abstract):
>> __tablename__ = 'concrete'
>> id = Column(Integer, primary_key=True)
>> # ---
>>
>> # DB creation
>> engine = create_engine('sqlite:///:memory:', echo=True)
>> Base.metadata.create_all(engine)
>
> Here's the sql call:
>> CREATE TABLE concrete (
>> id INTEGER NOT NULL,
>> PRIMARY KEY (id)
>> )
>
> What am I missing? Did I misunderstood how the mixin should be used?
> (I'm using SQLA 0.5rc4)
>
> Many thanks!
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---