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

Creating WebhooksEndpoint locally raises tolerance constraint failed error #1955

Closed
davemcphee opened this issue Jul 23, 2023 · 5 comments · Fixed by #1964
Closed

Creating WebhooksEndpoint locally raises tolerance constraint failed error #1955

davemcphee opened this issue Jul 23, 2023 · 5 comments · Fixed by #1964
Labels
webhook / sync issues Errors or Issues with webhooks, sync_from_stripe_data etc

Comments

@davemcphee
Copy link

davemcphee commented Jul 23, 2023

Describe the bug
Attempting to create new WebhookEndpoint via django admin fails with djstripe.models.webhooks.WebhookEndpoint.DoesNotExist: WebhookEndpoint matching query does not exist.

above error is caused by failure to create local object:

sqlite: sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance
mysql: MySQLdb.IntegrityError: (1048, "Column 'tolerance' cannot be null")

The WebhookEndpoint is successfully created in stripe, but not in the local DB.

To Reproduce
Steps to reproduce the behavior:

  1. Fresh django installation with dj-stripe
  2. python3 manage.py migrate && python3 manage.py runserver
  3. Add an API key via Django admin
  4. [optionally] run python3 manage.py djstripe_sync_models
  5. Add a new WebhookEndpoint via django admin

Expected behavior
A new WebhookEndpoint is created in local django app

Environment

  • dj-stripe version: 2.8.1
  • stripe API version: "2022-11-15"
  • Database: [any]
  • Python version: 3.11.4
  • Django version: 4.2.3

Can you reproduce the issue with the latest version of master?

Yes

It appears that the API does not return the tolerance key, so somewhere along the line this field is explicitly set to None, which eventually causes the error IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance (as opposed to the default value being used as defined in the model).

This issue can be easily reproduced by modifying the webhook fixture (tests/fixtures/webhook_endpoint_fake0001.json) to remove the tolerance key, and running pytest.

I'm very new to dj-stripe, but issue can be resolved by adding a check to models.webhooks.WebhookEndpoint._attach_objects_hook:

    def _attach_objects_hook(
        self, cls, data, current_ids=None, api_key=djstripe_settings.STRIPE_SECRET_KEY
    ):
        ...
        self.djstripe_uuid = data.get("metadata", {}).get("djstripe_uuid")

        if not isinstance(data.get("tolerance", None), (int, float)):
            self.tolerance = djstripe_settings.WEBHOOK_TOLERANCE

If this is actually a bug, and the proposed fix looks good, I'd be happy to contribute a PR.

@davemcphee davemcphee added the webhook / sync issues Errors or Issues with webhooks, sync_from_stripe_data etc label Jul 23, 2023
@davemcphee davemcphee changed the title Creating WebhooksEndpoint locally raises constraint failed error Creating WebhooksEndpoint locally raises tolerance constraint failed error Jul 23, 2023
@zzart
Copy link

zzart commented Aug 1, 2023

Had the same problem and this fixed it. +1

PabloCastellano added a commit to PabloCastellano/dj-stripe that referenced this issue Aug 7, 2023
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes dj-stripe#1955
PabloCastellano added a commit to PabloCastellano/dj-stripe that referenced this issue Aug 7, 2023
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes dj-stripe#1955
PabloCastellano added a commit to PabloCastellano/dj-stripe that referenced this issue Aug 7, 2023
StringDataRightTruncation: value too long for type character varying(15)

sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes dj-stripe#1955
PabloCastellano added a commit to PabloCastellano/dj-stripe that referenced this issue Aug 7, 2023
StringDataRightTruncation: value too long for type character varying(15)

sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes dj-stripe#1955
@PoDuck
Copy link

PoDuck commented Aug 9, 2023

I am having the same issue. PabloCastellano's fix works. When might this get merged? As of now, it is impossible to create webhooks that work until this fix is integrated.

PabloCastellano added a commit to PabloCastellano/dj-stripe that referenced this issue Aug 10, 2023
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes dj-stripe#1955
jleclanche pushed a commit that referenced this issue Aug 10, 2023
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes #1955
@KvyatkovskyAleksey
Copy link
Contributor

In description to 2.8 version:
DJSTRIPE_WEBHOOK_TOLERANCE is deprecated in favour of the new UUID webhooks tolerance model field.
but in code presented:

if not isinstance(data.get("tolerance", None), (int, float)):
            self.tolerance = djstripe_settings.WEBHOOK_TOLERANCE

so, probably need to change to:
self.tolerance = stripe.Webhook.DEFAULT_TOLERANCE
because now i have error:
AttributeError: 'DjstripeSettings' object has no attribute 'WEBHOOK_TOLERANCE'

@jleclanche
Copy link
Member

Thank you @KvyatkovskyAleksey.

jleclanche pushed a commit that referenced this issue Aug 29, 2023
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes #1955

(cherry picked from commit 470924b)
@rob-gonz
Copy link

This is still an issue. I had to do something and modify the site-packages/djstripe module to get this to work just like OP. I appreciate it.

Version: djstripe=2.8.1

jleclanche pushed a commit that referenced this issue Apr 25, 2024
sqlite3.IntegrityError: NOT NULL constraint failed: djstripe_webhookendpoint.tolerance

Fixes #1955
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
webhook / sync issues Errors or Issues with webhooks, sync_from_stripe_data etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants