oops, do it like this:

class BaseType(DeclarativeMeta):
    def __init__(newcls, classname, bases, dict_):
        dict_['notes'] = saschema.Column(satypes.String)
        DeclarativeMeta.__init__(newcls, classname, bases, dict_)


clearly we'll have to figure out a more user friendly pattern for this  
use case.

On Dec 19, 2008, at 5:27 AM, FrankB wrote:

>
> Hi,
>
> I have a similar task, so I tried to use your proposal, but it didn't
> work for me:
>
> ===
>
> from sqlalchemy import types as satypes
> from sqlalchemy import schema as saschema
> from sqlalchemy.engine import create_engine
> from sqlalchemy.ext.declarative import declarative_base,
> DeclarativeMeta
> from sqlalchemy.orm import scoped_session, sessionmaker
>
> class BaseType(DeclarativeMeta):
>    def __init__(newcls, classname, bases, dict_):
>        newcls.notes = saschema.Column(satypes.String)
>        DeclarativeMeta.__init__(newcls, classname, bases, dict_)
>
> MetaData = saschema.MetaData(bind=create_engine('sqlite:///:memory:'))
> Session = scoped_session(sessionmaker(bind=MetaData.bind))
> Base = declarative_base(metadata=MetaData, mapper=Session.mapper,
> metaclass=BaseType)
>
> class MasterEntity(Base):
>    __tablename__ = "master"
>    id         = saschema.Column(satypes.Integer, primary_key=True)
>    status     = saschema.Column(satypes.CHAR(length=1), default="A")
>
> print [ _c.key for _c in MasterEntity.__table__.columns ]
> # ['id', 'status']
> # => the 'notes' column is missing
>
> === snip
>
> So, what am I doing wrong?
>
> Thanks in advance,
>    Frank
>
> Btw.: Hello group and many thanks to Michael for this great piece of
> software!
>
>
>
> On Dec 16, 6:58 pm, Michael Bayer <[email protected]> wrote:
>> anabstractbaseclassisn't going to set up the same fields on all
>> descendants since the mapper()/Table() setup occurs during the
>> creation of the individualclassusing the non-inheritedclassdict,
>> 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:
>>
>> classMyMeta(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 anabstractclassto 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()
>>
>>>> classAbstract(object):
>>>>   notes = Column(String)
>>
>>>> classConcrete(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
-~----------~----~----~----~------~----~------~--~---

Reply via email to