Run this:

from sqlalchemy.orm import configure_mappers
configure_mappers()


that ensures RefClass's deferred mapping gets set up appropriately.



On 11/08/2016 01:39 PM, [email protected] wrote:
First, thanks for your very insightful response. I'm trying to reproduce
what you've provided, but I'm getting an `InvalidRequestError` when
querying on `RefClass`:
/InvalidRequestError: SQL expression, column, or mapped entity expected
- got '<class 'RefClass'>'
/

Indeed, RefClass has no `__mapper__` attribute. Querying on any other
object works as intended though. I imagine there's a small detail I'm
missing, so I've highlighted the changes from yesterday's post.


from sqlalchemy import (
    Column,
    ForeignKey,
    Integer,
    create_engine,
)
from sqlalchemy.ext.declarative import (
    AbstractConcreteBase,
    declared_attr,
    declarative_base,
    has_inherited_table,
)
from sqlalchemy.orm import Session

Base = declarative_base()


class AClass(Base):
    __tablename__ = 'aclass'
    id = Column(Integer, primary_key=True)


class BClass(Base):
    __tablename__ = 'bclass'
    id = Column(Integer, primary_key=True)


class RefClass(AbstractConcreteBase, Base):
    @declared_attr
    def __tablename__(cls):
        if cls.__name__ == 'RefClass':
            return None
        return cls.__name__.lower()

    @declared_attr.cascading
    def id(cls):
        if cls.__name__ == 'RefClass':
            return Column(Integer)
        column_name = '{}.id'.format(cls.ref.__tablename__)
        return Column(ForeignKey(column_name), primary_key=True)

    @declared_attr
    def __mapper_args__(cls):
        if cls.__name__ == 'RefClass':
            return {}
        return {
            'concrete': True,
            'polymorphic_identity': cls.ref.__name__
        }


class ARefClass(RefClass):
    ref = AClass


class BRefClass(RefClass):
    ref = BClass


engine = create_engine('sqlite://', echo=True)
Base.metadata.bind = engine
Base.metadata.create_all()
db = Session(engine)


Thanks!

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to