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

before_save not being executed #83

Open
HopeBaron opened this issue Mar 30, 2022 · 2 comments
Open

before_save not being executed #83

HopeBaron opened this issue Mar 30, 2022 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@HopeBaron
Copy link

HopeBaron commented Mar 30, 2022

class CreateTableMutation(mutations.DjangoCreateMutation):
    class Meta:
        model = Table
        exclude_fields = ('owner',)
    create_column = CreateTableColumnMutation.Field()

    @classmethod
    def before_save(cls, root, info, input, obj: Table):
        obj.owner = User.objects.get(pk=1)
        return obj

I've the following code, before_save doesn't get executed (I copied the example in docs)

this is what I get:

{
  "errors": [
    {
      "message": "NOT NULL constraint failed: query_builder_table.owner_id",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createTable"
      ]
    }
  ],
  "data": {
    "createTable": null
  }
}

It's supposed to provide the owner object through the before_save, right?

@demsking
Copy link

demsking commented Nov 1, 2023

Is there any update about this issue?

@tOgg1
Copy link
Owner

tOgg1 commented Feb 27, 2024

Hi. I'm sorry for the delayed response.

This is a bug. If you want a quick potential fix, I would move this handling to before_mutate as so:

@classmethod
def before_mutate(cls, root, info, input):
    input["owner"] = 1
    return input

The issue here is that for create mutations, before_save is not called until after the first object instantiation and save (by use of the .create method).

Similarly, for update mutations, a .save call is also applied before the before_save call.

I think the naming set by the library for these hooks is unfortunate. However, I think trying to remove all save calls in the internal methods (.create_obj and .update_obj) might be tricky and impose breaking changes. But either that has to happen, or the naming has to change.

I'll put some priority on resolving this task and keep you posted on which direction I move in.

@tOgg1 tOgg1 added the bug Something isn't working label Feb 27, 2024
@tOgg1 tOgg1 self-assigned this Feb 27, 2024
@tOgg1 tOgg1 added this to the v0.12.0 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants