[Sqlalchemy-tickets] Issue #4091: declared_attr.cascading unconditionally sets attribute (zzzeek/sq
Brought to you by:
zzzeek
From: David L. <iss...@bi...> - 2017-09-26 04:26:26
|
New issue 4091: declared_attr.cascading unconditionally sets attribute https://bitbucket.org/zzzeek/sqlalchemy/issues/4091/declared_attrcascading-unconditionally David Lord: I'm not sure if this is a bug or just a result of unclear documentation. I would expect that a cascading declared attr would not overwrite an attribute that's manually defined on a class, but that's not the case. This makes it less useful for things like `__tablename__` (with #4090), where I would like the user to be able to set their own tablename to override the default. ```python3 import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base, declared_attr, \ has_inherited_table class Base: @declared_attr.cascading def id(cls): if has_inherited_table(cls): type = sa.ForeignKey(cls.__mro__[1].id) else: type = sa.Integer return sa.Column(type, primary_key=True) Base = declarative_base(cls=Base) class User(Base): __tablename__ = 'user' class User2(User): __tablename__ = 'user2' # due to cascading, the created name is 'id' instead of 'user_id' id = sa.Column('user_id', sa.ForeignKey(User.id), primary_key=True) engine = sa.create_engine('sqlite://', echo=True) Base.metadata.create_all(engine) ``` |