-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding @declared_attr __tablename__ to Mixin is overridden by _BoundDeclarativeMeta #323
Comments
Would you produce a full test case for this? Also, ensure that you are using the latest version of SQLAlchemy 1.0, 0.9, or 0.8 branches, and specify which you're using. |
import re
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import (Column, ForeignKey, Integer)
from sqlalchemy.ext.declarative import declared_attr
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
db = SQLAlchemy(app)
class PAFMixin(object):
@declared_attr
def __tablename__(cls):
return 'paf' + re.sub(
'[A-Z]', lambda match: '_' + match.group(0).lower(), cls.__name__)
class Locality(PAFMixin, db.Model):
locality_id = Column(Integer, primary_key=True)
class Address(PAFMixin, db.Model):
address_id = Column(Integer, primary_key=True)
locality_id = Column(Integer, ForeignKey('paf_locality.locality_id'))
db.create_all() Results in:
Hopefully this is sufficient information? |
I also encountered this, and have a pair of test cases (909bb21)in my mixin-tablenames branch. The second commit (19fc6ab) resolves the test cases, but it feels like a really hacky solution. |
Flask-SQLAlchemy was overriding __tablename__ set in mixins. Upgrading to more recent version should also fix, but would require more re-work due to other changes. pallets-eco/flask-sqlalchemy#323
Using 2.0
I was using a mixin on my models to add a
__tablename__
method to work out a prefixed/namespaced table name based on the class name but_BoundDeclarativeMeta
's doesn't appear to check against base classes before adding its own.This lead to errors like:
NoReferencedTableError: Foreign key associated with column 'not_yet_built.dependent_thoroughfare_id' could not find table 'paf_thoroughfare' with which to generate a foreign key to target column 'thoroughfare_id'
Note that the class would prefix the table-name with paf, but this was not present in the first name of the exception (the latter is the reference from the
ForeignKey
itself). This prevents namespacing on systems that don't support schema (MySQL) and could be very confusing for others.The text was updated successfully, but these errors were encountered: