Hi, 

I have a mixin like:

class BasicSqlClass(object):

    @declared_attr
    def __tablename__(cls):
        return convert(cls.__name__) + 's


The mixin declares the table name and does other stuff.

Hover, there is a class where I wanted to overwrite the mixed-in 
__tablename__ by declaring the __tablename__ for the class like.

class  SpecialClass(BasicSqlClass, db.Model):

     __tablename__ = 'other_table_name'


However, that didn't work.  the mixed-in __tablename__ was still used.  I 
also tried writing a declared attribute for the SpecialClass, but that 
didn't overwrite the mixin either.

class  SpecialClass(BasicSqlClass, db.Model):

    @declared_attr
    def __tablename__(cls):
        return 'other_table_name'


I ended up making a special mixin to overwrite the __tablename__.

class OverwriteTableName(object):
    #I needed to make a class to overwrite the table name

    __tablename__ = 'other_table_name'


class  SpecialClass(OverwriteTableName, BasicSqlClass, db.Model):



The special mixin worked, though I was surprised I was not able to 
overwrite the mixed-in __tablename__ like a regular inherited attribute.

Any thoughts on this?  If the behavior is expected or intended, If perhaps 
a warning about this behavior could be added to the documentation that 
would be great.  I've written a comment below, but I don't know how to 
propose it's addition to the documentation.

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">
Mixed-In declared attributes can only be overridden by other mixins.  A 
declared attribute inherited from a mixed-in class cannot be overridden 
directly.  For example, a class has a __tablename__ attribute inherited 
from a mixin where __tablename__ is defined as a declared attribute.  Just 
adding __tablename__ to the class is insufficient to overwrite the 
__tablename__ inherited from the mixin for the class.  A second mixin with 
the __tablename__ attribute must be declared and added to the class with 
inheritance priority over the mixin.
</p>
</div>

I'm thinking it would go at:

https://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html
https://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/api.html#sqlalchemy.ext.declarative.declared_attr


Thanks!

~Victor




-- 
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