Sorry, was out due to moving... BLEH.
I like second solution since I then don't need to declare the relationship
on every sub class.
Basicly:
1. Configure as I had previously
2. Make sure configure_mappers() is ran after all sub classes are
declared.
3. Monkey Wrench the base abstract class so it can also be queried on
the relationship.
# See code above..
import sqlalchemy
sqlalchemy.orm.configure_mappers()
AbstractConcreteAbstraction.something = relationship(Something)
Nice work! :)
On Wednesday, February 27, 2013 8:44:00 PM UTC-6, Michael Bayer wrote:
>
> simpler, just stick the relationship on ACA:
>
> session = Session(engine)
> print session.query(ConcreteConcreteAbstraction).filter(
> ConcreteConcreteAbstraction.something.has(id=1)).all()
>
> AbstractConcreteAbstraction.something = relationship(Something)
> print session.query(AbstractConcreteAbstraction).filter(
> AbstractConcreteAbstraction.something.has(id=1)).all()
>
>
>
> On Feb 27, 2013, at 9:41 PM, Michael Bayer
> <[email protected]<javascript:>>
> wrote:
>
> just features that weren't anticipated (I never use concrete inheritance).
> here's what will work for now.
>
> class AbstractConcreteAbstraction(AbstractConcreteBase, sqlite):
> __table_args__ = (UniqueConstraint('derpa',
> 'derp'),)
> id = Column(Integer, primary_key=True)
> derpa = Column(Integer)
> derp = Column(Integer)
>
> @declared_attr
> def something_id(cls):
> return Column(ForeignKey(Something.id))
>
> class ConcreteConcreteAbstraction(AbstractConcreteAbstraction):
> __tablename__ = u'cca'
> __mapper_args__ = {'polymorphic_identity': 'ccb',
> 'concrete': True}
>
> something = relationship(Something)
>
> import sqlalchemy
> sqlalchemy.orm.configure_mappers()
>
> AbstractConcreteAbstraction.something = relationship(Something)
>
> sqlite.metadata.create_all()
>
> # Works
> print session.query(ConcreteConcreteAbstraction).filter(
> ConcreteConcreteAbstraction.something.has(id=1)).all()
> # Don't work
> print session.query(AbstractConcreteAbstraction).filter(
> AbstractConcreteAbstraction.something.has(id=1)).all()
>
>
>
> On Feb 27, 2013, at 8:06 PM, Derek Litz <[email protected] <javascript:>>
> wrote:
>
> Having fun with AbstractBaseClasses tonight :) ... Anyways am I missing
> something here as well?
>
> I tried playing with querying the AbstractBaseClass and filtering on sub
> classes but that just produced
> a query that did not execute.
>
> from sqlalchemy.engine import Engine
> from sqlalchemy import event
> from sqlalchemy import (Column, Integer, Unicode, DateTime, ForeignKey,
> Boolean, Numeric, Time)
>
>
> # Taken from http://docs.sqlalchemy.org/ru/latest/dialects/sqlite.html
> @event.listens_for(Engine, "connect")
> def set_sqlite_pragma(dbapi_connection, connection_record):
> cursor = dbapi_connection.cursor()
> cursor.execute("PRAGMA foreign_keys=ON")
> cursor.close()
>
>
> from sqlalchemy import create_engine
> from sqlalchemy.ext.declarative import (declarative_base, declared_attr,
> AbstractConcreteBase)
> from sqlalchemy.orm import sessionmaker, relationship, backref,
> object_session
>
> engine = create_engine('sqlite:///test.db')
> sqlite = declarative_base(bind=engine)
> get_session = sessionmaker(bind=engine)
> session = get_session()
>
> from sqlalchemy.schema import UniqueConstraint
>
>
> class Something(sqlite):
> __tablename__ = u'something'
> id = Column(Integer, primary_key=True)
>
>
> class AbstractConcreteAbstraction(AbstractConcreteBase, sqlite):
> __table_args__ = (UniqueConstraint('derpa',
> 'derp'),)
> id = Column(Integer, primary_key=True)
> derpa = Column(Integer)
> derp = Column(Integer)
>
> @declared_attr
> def something_id(cls):
> return Column(ForeignKey(Something.id))
>
> @declared_attr
> def something(cls):
> return relationship(Something)
>
>
> class ConcreteConcreteAbstraction(AbstractConcreteAbstraction):
> __tablename__ = u'cca'
> __mapper_args__ = {'polymorphic_identity': 'ccb',
> 'concrete': True}
>
>
> import sqlalchemy
> sqlalchemy.orm.configure_mappers()
> sqlite.metadata.create_all()
>
> # Works
> print session.query(ConcreteConcreteAbstraction).filter(
> ConcreteConcreteAbstraction.something.has(id=1)).all()
> # Don't work
> print session.query(AbstractConcreteAbstraction).filter(
> AbstractConcreteAbstraction.something.has(id=1)).all()
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.