#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Unhappy How to define metaclass for a class that extends from sqlalchemy declarative base


    I use: Python 2.6 and sqlalchemy 0.6.1

    This is what I am trying to do:

    Code:
    from sqlalchemy.types import (
        Integer,
        String,
        Boolean
    )
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    class SampleMeta(type):
        def __new__(cls, name, bases, attrs):
            attrs.update({   'id': Column('Id', Integer, primary_key=True),
                        'name': Column('Name', String),
                        'description': Column('Description', String),
                        'is_active': Column('IsActive', Boolean)
                    })
            return super(SampleMeta, cls).__new__(cls, name, bases, attrs)
    
    
    class Sample(Base):
        __tablename__ = 'Sample'
        __table_args__ = {'useexisting': True}
        __metaclass__ = SampleMeta
    
        def __init__(self, id, name, description, is_active):
            self.id = id
            self.name = name
            self.description = description
            self.is_active = is_active
    
        def __repr__(self):
            return "<(%d, '%s', '%s', %r)>" % (self.id, self.name, self.description, self.isactive)

    And the error I am getting is this:

    Code:
    TypeError: Error when calling the metaclass bases
        metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
    Now, if I do the same thing above by using

    Code:
    class Sample(object)
    instead of

    Code:
    class Sample(Base)
    it works absolutely fine.

    I need to update the attributes of the class dynamically. So, I will be using dynamic attribute and column names. And I need the above piece code to work in order to be able to get there.

    Please help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    11
    What are you trying to accomplish in a more general sense? It looks like you might be cutting against the grain here, so a little more context might be helpful (and generate more interest in your question).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Inherit from sqlalchemy.ext.declarative.DeclarativeMeta rather than from type.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0
    Originally Posted by Nyktos
    Inherit from sqlalchemy.ext.declarative.DeclarativeMeta rather than from type.
    Perfect. It works. Thanks a lot.

IMN logo majestic logo threadwatch logo seochat tools logo