Skip to content

POST /store/<store_id>/tag fails on Render with Marshmallow #4

@SANDEEPNEGI07

Description

@SANDEEPNEGI07

Description

When deploying the project to Render, the POST /store/<store_id>/tag endpoint crashes.
The same request works locally with insomnia client, but fails on Render due to some Marshmallow behavior.

Steps to Reproduce

  1. Deploy the project to Render.
  2. Send a POST request to /store/1/tag with body:
{
  "name": "Food"
}

The server responds with an error:

'dict' object has no attribute '_sa_instance_state'

Solution

@blp.response(200, TagSchema)    # Response can include items and store
def post(self, tag_data, store_id):
    if TagModel.query.filter(
        TagModel.store_id == store_id,
        TagModel.name == tag_data["name"]
    ).first():
        abort(400, message="A tag with that name already exists in that store.")

    tag = TagModel(**tag_data, store_id=store_id)

    try:
        db.session.add(tag)
        db.session.commit()
    except SQLAlchemyError as e:
        abort(500, message=str(e))

    return tag

The correct fix is to separate schemas (PlainTagSchema for input, TagSchema for output).

I hope this bug solution will help you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions