Marshmallow: RecursionError: maximum recursion depth exceeded while calling a Python object

Created on 22 Jun 2019  Â·  4Comments  Â·  Source: marshmallow-code/marshmallow

Hey... I have a problem with many to many self referencing here ! Thx for answering.

schema

class UserSchema(Schema):
    username = fields.Str()
    email = fields.Str()
    posts = fields.Nested(PostSchema, many=True)
    about_me = fields.Str()
    last_seen = fields.DateTime()
    followed = fields.Nested("self", many=True)

model SQLALCHEMY

followers = db.Table(
    "followers",
    db.Column(
        "follower",
        db.Integer,
        db.ForeignKey("user.id"),
        primary_key=True,
    ),
    db.Column(
        "followed",
        db.Integer,
        db.ForeignKey("user.id"),
        primary_key=True,
    ),
)

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True,)
    username = db.Column(db.String(128), unique=True,)
    password = db.Column(db.String(512), )
    email = db.Column(db.String(128), unique=True, )
    posts = db.relationship("Post", backref="author", lazy="dynamic")
    about_me = db.Column(db.String(256))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    followed = db.relationship(
                    "User",
                    secondary=followers,
                    primaryjoin=id==followers.c.follower,
                    secondaryjoin=id==followers.c.followed,
                    backref=db.backref("followers", lazy="dynamic"),
                    lazy="dynamic",
                )

Error

  File "/home/pd/gits/microblog/venv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 1330, in _generate_generic_binary
    + binary.right._compiler_dispatch(
  File "/home/pd/gits/microblog/venv/lib/python3.6/site-packages/sqlalchemy/sql/visitors.py", line 91, in _compiler_dispatch
    return meth(self, **kw)
  File "/home/pd/gits/microblog/venv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 1455, in visit_bindparam
    impl = bindparam.type.dialect_impl(self.dialect)
  File "/home/pd/gits/microblog/venv/lib/python3.6/site-packages/sqlalchemy/sql/type_api.py", line 446, in dialect_impl
    return dialect._type_memos[self]["impl"]
  File "/usr/lib/python3.6/weakref.py", line 394, in __getitem__
    return self.data[ref(key)]
RecursionError: maximum recursion depth exceeded while calling a Python object

question

Most helpful comment

You may need to use exclude=("followed",) to prevent infinite recursion. See https://marshmallow.readthedocs.io/en/3.0/nesting.html#nesting-a-schema-within-itself .

Closing this for now, but feel free to comment if you're still running into issues.

All 4 comments

Isn't this a sqlalchemy or marshmallow-sqlalchemy issue?

@lafrech everything works well until adding followed = fields.Nested("self", many=True), actually many=True make this happens.
the project is here

You may need to use exclude=("followed",) to prevent infinite recursion. See https://marshmallow.readthedocs.io/en/3.0/nesting.html#nesting-a-schema-within-itself .

Closing this for now, but feel free to comment if you're still running into issues.

What if I need like 4 or 5 levels but not ∞? exclude seems to fix my Swagger but breaks my deep-nested integration test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manoadamro picture manoadamro  Â·  3Comments

imhoffd picture imhoffd  Â·  3Comments

m-novikov picture m-novikov  Â·  3Comments

zohuchneg picture zohuchneg  Â·  3Comments

nickretallack picture nickretallack  Â·  4Comments