question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLAlchemyObjectType hides original SQLAlchemy exception

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Reactions:33
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
cmcinroycommented, May 28, 2018

This really makes troubleshooting model issues very difficult. I hit this problem on a regular basis.

10reactions
vietvudanhcommented, Mar 8, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Core Exceptions - SQLAlchemy 1.4 Documentation
Exceptions used with SQLAlchemy. The base exception class is SQLAlchemyError . Exceptions which are raised as a result of DBAPI exceptions are all...
Read more >
graphene-sqlalchemy 2.3.0 - PythonFix.com
Graphene SQLAlchemy integration. ... SQLAlchemyObjectType hides original SQLAlchemy exception; Add SQLAlchemyList and SQLAlchemy mutations ...
Read more >
How to catch specific exceptions on sqlalchemy?
You can reraise the original exception from the except block and catch whatever specific type you are interested in: import sqlalchemy ...
Read more >
graphql-python - Bountysource
Here's a minimal code that explains the issue. import graphene from graphene_sqlalchemy import SQLAlchemyObjectType from sqlalchemy import Column, Integer from ...
Read more >
SQLAlchemy + Flask Tutorial - Graphene-Python
flask_sqlalchemy/schema.py import graphene from graphene import relay from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found