Created
June 19, 2012 12:19
-
-
Save medwards/2953833 to your computer and use it in GitHub Desktop.
This produces an AssertionError. Why?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
engine = create_engine('sqlite:///test.db', echo=True) | |
Session = sessionmaker(bind=engine) | |
session = Session() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base import Thing | |
#from otherthings import * # Need this or AssertionError | |
import db | |
print db.session.query(Thing).all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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