SQLAlchemyObjectType hides original SQLAlchemy exception
See original GitHub issueHi all!
Here’s a minimal code that explains the issue.
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a = relationship(A) # This is the breaking line
class BSchema(SQLAlchemyObjectType):
class Meta:
model = B
schema = graphene.Schema(query=BSchema)
When I try to run this file I only get this error:
Traceback (most recent call last):
File "issue.py", line 22, in <module>
class BSchema(SQLAlchemyObjectType):
File "./.venv/lib/python3.6/site-packages/graphene/utils/subclass_with_meta.py", line 40, in __init_subclass__
super_class.__init_subclass_with_meta__(**options)
File "./.venv/src/graphene-sqlalchemy/graphene_sqlalchemy/types.py", line 97, in __init_subclass_with_meta__
).format(cls.__name__, model)
AssertionError: You need to pass a valid SQLAlchemy Model in BSchema.Meta, received "<class '__main__.B'>".
But I’m passing a valid SQLAlchemy Model in BSchema.Meta.
The problem is actually another.
SQLAlchemy is raising this exception:
{NoForeignKeysError}Could not determine join condition between parent/child tables on relationship B.a - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
But this exception is lost here https://github.com/graphql-python/graphene-sqlalchemy/blob/a2fe9263d73b3baccd63b192b983cda5abfb88f0/graphene_sqlalchemy/utils.py#L21-L27 (called from https://github.com/graphql-python/graphene-sqlalchemy/blob/a2fe9263d73b3baccd63b192b983cda5abfb88f0/graphene_sqlalchemy/types.py#L94) making the debug process for a simple mistake a pain.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:33
- Comments:14 (5 by maintainers)

Top Related StackOverflow Question
This really makes troubleshooting model issues very difficult. I hit this problem on a regular basis.
Why does this line not at least print out the stack trace? I spent 1 hours finding what the root cause before find out this issue.