[Sqlalchemy-tickets] Issue #3262: Warn on duplicate polymorphic_identity (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Sebastian B. <iss...@bi...> - 2014-12-02 14:32:34
|
New issue 3262: Warn on duplicate polymorphic_identity https://bitbucket.org/zzzeek/sqlalchemy/issue/3262/warn-on-duplicate-polymorphic_identity Sebastian Bank: 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): ```python 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 # fails ``` Provided there is no use case for overwriting, it would be nice to make the user aware of such misconfiguration at an early development stage. |