Skip to content

SQLAlchemyObjectType hides original SQLAlchemy exception #121

Closed
@Fedalto

Description

@Fedalto

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

def is_mapped_class(cls):
try:
class_mapper(cls)
except (ArgumentError, UnmappedClassError):
return False
else:
return True
(called from
assert is_mapped_class(model), (
)
making the debug process for a simple mistake a pain.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions