Skip to content
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

SQLAlchemy relationship attribute with collection_class option #113

Open
dabajabaza opened this issue Nov 5, 2021 · 2 comments
Open

SQLAlchemy relationship attribute with collection_class option #113

dabajabaza opened this issue Nov 5, 2021 · 2 comments

Comments

@dabajabaza
Copy link

The bug
I have sqlalchemy model with relationship attribute states and collection_class option.
I define colanderalchemy schema that NOT includes attribute states, so I expect that this attribute would be ignored even if it's exists in input data.
Colanderalchemy generates error while trying to objectify input data with states in it.

Expected behavior
Schema objectified successfully without errors. The states attribute is ignored.

To Reproduce

I used the following code:

from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (
    BigInteger,
    Column,
    ForeignKey,
    Unicode,
)
from colanderalchemy import SQLAlchemySchemaNode

Base = declarative_base()

class State(Base):
    __tablename__ = "State"
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    code = Column(Unicode(50), nullable=False, unique=True)
    name = Column(Unicode(50), nullable=False)


class Module(Base):
    __tablename__ = "Module"
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    name = Column(Unicode(50), nullable=False)
    state_id = Column(ForeignKey(State.id))
    states = relationship(State,
                          uselist=True,
                          collection_class=attribute_mapped_collection("code"),
                          )


schema = SQLAlchemySchemaNode(
    Module,
    includes=[
        "name",
    ],
    title="Module schema",
)
schema.objectify({"name": "Some Name", "states": {}})
Traceback (most recent call last):
  File "src/script.py", line 44, in <module>
    schema.objectify({"name": "Some Name", "states": {}})
  File "env\lib\site-packages\colanderalchemy\schema.py", line 706, in objectify
    setattr(context, attr, value)
  File "env\lib\site-packages\sqlalchemy\orm\attributes.py", line 459, in __set__
    self.impl.set(
  File "env\lib\site-packages\sqlalchemy\orm\attributes.py", line 1548, in set
    raise TypeError(
TypeError: Incompatible collection type: list is not dict-like

Versions

  • OS: Windows 10
  • Python: 3.8.10
  • SQLAlchemy: 1.4.26
  • colander: 1.8.3
  • ColanderAlchemy: 0.3.4
  • Database: Microsoft SQL Server 2019
@tisdall
Copy link
Collaborator

tisdall commented Nov 29, 2021

I'm not sure if this is helpful, but shouldn't it be schema.objectify({"name": "Some Name", "states": []}) so states is a list?

1 similar comment
@tisdall
Copy link
Collaborator

tisdall commented Nov 29, 2021

I'm not sure if this is helpful, but shouldn't it be schema.objectify({"name": "Some Name", "states": []}) so states is a list?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants