On Aug 15, 2013, at 9:37 AM, Luke <luke.b3...@gmail.com> wrote: > Hey, > how may i extend the declerative base to provide default things that are > always available to any class/table that is derived from it (like primary > keys, create timestamp columns ect) ? > i tried something like this: > > import datetime > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy import Column, Integer, String > > Base = declarative_base() > > class NewBase(Base): > __tablename__ = "default" > id = Column(Integer, primary_key=True) > created = Column(DateTime, nullable=False, default=datetime.datetime.now) > > class NewCustomTable(NewBase): > __tablename__ = "NewCustomTable" > awesome = Column(String) > > but i'm getting either sqlalchemy.exc.NoForeignKeysError or > sqlalchemy.exc.InvalidRequestError depending on declaring the __tablename__ > attribute or not. > > with __tablename__ : > sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships > between 'default' and 'NewCustomTable'. > > without: > sqlalchemy.exc.InvalidRequestError: Class <class 'NewBase'> does not have a > __table__ or __tablename__ specified and does not inherit from an existing > table-mapped class.
if you want your NewBase to be a descendant of Base, then you'd need to put __abstract__=True on it. But if these cols are global to everyone you could make it the superclass of your declarative Base also by passing it as "class_" to declarative_base(). the naming conventions recipe is another way to go too.
signature.asc
Description: Message signed with OpenPGP using GPGMail