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

Trigger listeners cannot handle models with custom db_column attribute #20

Closed
PaulGilmartin opened this issue Jan 19, 2023 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@PaulGilmartin
Copy link
Owner

PaulGilmartin commented Jan 19, 2023

See #19 for more

@PaulGilmartin
Copy link
Owner Author

Issue comes from the fact that a Notification coming from a trigger sends a payload built as follows:

    def _build_payload(self, model):
        return  f'''
            payload := json_build_object(
                'app', '{model._meta.app_label}',
                'model', '{model.__name__}',
                'old', row_to_json(OLD),
                'new', row_to_json(NEW)
              );
        '''

Here, OLD and NEW will build JSON according to the columns as they're stored in the database (i.e. will use the db_column value on a ForeignKey if set). We then attempt to deserialise that value in the TriggerPayload class in order to build the old and new arguments to our trigger based listener. Our deserialisation method looks as follows:

class TriggerPayload:
    def __init__(self, payload: Dict):
        self._json_payload = payload
        self._model = apps.get_model(
            app_label=self._json_payload['app'],
            model_name=self._json_payload['model'],
        )
        self._old_row_data = self._json_payload['old']
        self._new_row_data = self._json_payload['new']

    @property
    def old(self):
        if self._old_row_data:
            return self._model(**self._old_row_data)

The error comes in the line self._model(**self._old_row_data): since self._old_row_data was built by the aformentioned trigger payload (which uses the database column names), if a model has a FK attribute which does not match the corresponding db column name, we'll get an error like

 Encountered exception Author() got unexpected keyword arguments: 'some_other_column_name'

@PaulGilmartin
Copy link
Owner Author

This has been fixed in #34 and is available in v1.0.5

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

1 participant