Skip to content

Instantly share code, notes, and snippets.

@medwards
Created June 19, 2012 12:19
Show Gist options
  • Save medwards/2953833 to your computer and use it in GitHub Desktop.
Save medwards/2953833 to your computer and use it in GitHub Desktop.
This produces an AssertionError. Why?
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import Column, Integer, String, create_engine
Base = declarative_base()
class Thing(Base):
__tablename__ = 'things'
id = Column(Integer, primary_key=True)
name = Column(String)
discriminator = Column('type', String)
@declared_attr
def __mapper_args__(cls):
args = {}
args['polymorphic_on'] = 'discriminator'
args['polymorphic_identity'] = cls.__name__.lower()
return args
def __init__(self, name):
self.name = name
import db
import base
import otherthings
base.Base.metadata.drop_all(base.engine)
base.Base.metadata.create_all(base.engine)
things = [otherthings.Foo('hoohaa'), otherthings.Bar('woop woop')]
for thing in things:
db.session.add(thing)
db.session.commit()
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///test.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
from base import Thing
#from otherthings import * # Need this or AssertionError
import db
print db.session.query(Thing).all()
from base import Thing
class Foo(Thing):
def custom_behaviour(self):
print self.name
class Bar(Thing):
def calculate_stuff(self):
print self.name * 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment