-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
Milestone
Description
Migrated issue, originally created by Sebastian Bank (@xflr6)
Currently, reuse of a value for polymorphic_identity just overwrites the old mapping.
This can lead hard to debug failiures (queried instances having unexpected types):
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
class Spam(declarative_base()):
__tablename__ = 'spam'
id = Column(Integer, primary_key=True)
kind = Column(String)
__mapper_args__ = {'polymorphic_on': kind, 'polymorphic_identity': 'spam'}
class Eggs(Spam):
__tablename__ = 'eggs'
id = Column(Integer, ForeignKey('spam.id'), primary_key=True)
__mapper_args__ = {'polymorphic_identity': 'spam'} # warn here
engine = create_engine('sqlite://')
Spam.metadata.create_all(engine)
session = Session(engine)
session.add(Spam())
session.flush()
spam = session.query(Spam).one()
assert type(spam) is Spam # failsProvided there is no use case for overwriting, it would be nice to make the user aware of such misconfiguration at an early development stage.
Reactions are currently unavailable