down votefavorite 
<http://stackoverflow.com/questions/42918152/sqlalchemy-polymorphism-without-discriminators#>

I am trying to use an external library which defines a class model in my 
own program. I want the classes I define to be the same in all respects to 
their parents from the library, except that I want to append some helper 
methods to my local extensions. For example:

*External Library:*

Base = declarative_base()class BaseUser(Base):
    __tablename__ = 'user'

    email = Column(String(100), nullable=False, unique=True)
    password = Column(String(128), nullable=False)

    address_uid = Column(Integer, ForeignKey('address.uid'))
    address = relationship('BaseAddress', back_populates="users")

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.address = BaseAddress()
class BaseAddress(Base):
    __tablename__ = 'address'

    street = Column(String(100))
    unit = Column(String(32))
    city = Column(String(64))
    state = Column(String(32))
    postal = Column(String(32))
    country = Column(String(32))

    users = relationship('user', back_populates="address")

*Local model:*

class User(BaseUser):
    def in_country(county):
        return self.address.country == country
class Address(BaseAddress):
    pass

The goal here is to create subclasses which sqlalchemy need not distinguish 
from their parents. If I insert an Address into User.address, for example, 
sqlalchemy should not complain about a type mismatch (Address instead of 
the expected BaseAddress).

The only way of doing this that I can discern would involve using 
polymorphic_on in the parent classes. I don't want to do this, because it 
doesn't accurately model what is happening. It would require a 
discriminator, and it might behave strangely in the event I used a 
migration script locally. Is there a way with sqlalchemy to achieve 
polymorphism (I think it's called "ad-hoc polymorphism") *without* using 
discriminators, or some other way of achieving my goal?

*Oblig:*

Special thanks to everyone who participants in this group. I have followed 
it, though haven't contributed to it.  I have created a SO post for this as 
well here in case anyone wants to rack up a few 
points: 
http://stackoverflow.com/questions/42918152/sqlalchemy-polymorphism-without-discriminators

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to