Closed david-freistrom closed 5 years ago
As far as I can tell, class_mapper()
is raising an error because sqlalchemy
is not able to map your model classes. I would recommend that you write a test for your models and iteratively add / remove columns / relationship definitions until you find the root cause.
I'm closing this issue because it does not seem to have anything to do with graphene-sqlalchemy
.
note that the model as given works fine in an isolated test case without all the flask/graphene-sqlalchemy imports added, so the model as given is fine, I ran it and it has no issues. someone needs to pdb into the exception to see what the ultimate AttributeError (I assume it's an AttributeError) is being raised.
Ran into exactly the same error multiple times and it is driving me crazy! SQLAlchemy itself passed all tests. It only shows this error when combined with graphene.
For one time, removing back_populations
and remove the relationship from one side made the error disappear. For another time, a simple import statement of the model caused this error.
did anyone try calling configure_mappers() at the right time so that backrefs are all set up?
I eventually found the answer. The reason for the error is that the model resolution needs the model to be imported somewhere before. So try to import the model in the beginning of the app fixed my issue.
The accepted answer in this stack overflow hit the nail on the head: https://stackoverflow.com/questions/45534903/python-sqlalchemy-attributeerror-mapper/45540141#45540141
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.
Python 3.7.4 SQLAlchemy 1.3.7 graphene-sqlalchemy 2.2.2 Flask 1.1.1 Flask-SQLAlchemy 2.4.0 psycopg2 2.8.3
Linux xxx 5.1.21-200.fc29.x86_64 #1 SMP Mon Jul 29 15:30:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
The following code snippets show just a simple One-to-Many Relationship. I tried it with and back_populates and with backref, Uni- and Bidirectional. I also tried Many-to-Many relationships with a association Table. But nothing helped.
I always get the error shown in the last Bash-snippet.
Whats wrong here? I found out, that sqlalchemy.orm.relationships.RelationshipProperty give that exception when I try to call .entity on it.
I already opened an issue on the sqlalchemy github https://github.com/sqlalchemy/sqlalchemy/issues/4819 and got the answer above. Hopefully it helps you to help me to fix this issue ;)
It is happen in sqlalchemy/orm/mapper.py(1947)_post_configure_properties() on line 1947
class User(db.Model): tablename = "users"
id = Column(Integer, primary_key=True, autoincrement=True) first_name = Column(String(255), nullable=False) last_name = Column(String(255), nullable=False) email = Column(String(255), unique=True, nullable=False) public_key = Column(Text, unique=True) _secret_access_key = Column(Binary(60), unique=True) access_key_id = Column(String(255), unique=True) active = Column(Boolean()) confirmed_at = Column(DateTime()) confirmation_token = Column(String(255), unique=True) confirmation_sent_at = Column(DateTime()) role_id = Column(Integer, ForeignKey("roles.id"), nullable=False)
@hybrid_property def secret_access_key(self): return self._secret_access_key
@secret_access_key.setter def secret_access_key(self, plaintext_key): self._secret_access_key = bcrypt.generate_password_hash(plaintext_key, 15)
@hybrid_method def is_correct_secret_access_key(self, plaintext_key): return bcrypt.check_password_hash(self.secret_access_key, plaintext_key)
def repr(self): return '<User %r %r>' % (self.first_name, self.last_name)
all = [ User ]