Accounts fail to sync and APIKeys are not able to be inserted
See original GitHub issueDescribe the bug
Accounts and API keys do not seem to be created when running python manage.py djstripe_sync_models
.
I believe this is preventing all other models from synchronizing and any database saves from occurring for djstripe models.
To Reproduce
Install as specified in docs.
Run python manage.py djstripe_sync_models
Log output here
Skipping <class 'djstripe.models.base.IdempotencyKey'>: not a StripeModel
Skipping <class 'djstripe.models.api.APIKey'>: no stripe_class
Syncing Account:
(no results)
Skipping <class 'djstripe.models.billing.DjstripeInvoiceTotalTaxAmount'>: not a StripeModel
Skipping <class 'djstripe.models.billing.DjstripeUpcomingInvoiceTotalTaxAmount'>: not a StripeModel
Syncing Coupon:
(no results)
Syncing Invoice:
(no results)
Skipping <class 'djstripe.models.billing.UpcomingInvoice'>: Upcoming Invoices are virtual only
Syncing InvoiceItem:
(no results)
Syncing Plan:
(no results)
Syncing Subscription:
(no results)
Syncing SubscriptionItem:
(no results)
Syncing SubscriptionSchedule:
(no results)
Skipping <class 'djstripe.models.billing.TaxId'>: no stripe_class.list
Syncing TaxRate:
(no results)
Skipping <class 'djstripe.models.billing.UsageRecord'>: no stripe_class.list
Syncing Session:
(no results)
Syncing ApplicationFee:
(no results)
Skipping <class 'djstripe.models.connect.ApplicationFeeRefund'>: no stripe_class
Syncing CountrySpec:
(no results)
Syncing Transfer:
(no results)
Syncing TransferReversal:
(no results)
Syncing BalanceTransaction:
(no results)
Syncing Charge:
(no results)
Syncing Product:
(no results)
Syncing Customer:
Customer matching query does not exist.
Synced 1 Customer
Syncing Dispute:
(no results)
Syncing Event:
(no results)
Syncing FileUpload:
(no results)
Syncing PaymentIntent:
(no results)
Syncing SetupIntent:
(no results)
Syncing Payout:
(no results)
Syncing Price:
Product matching query does not exist.
Synced 1 Price
Syncing Refund:
(no results)
Skipping <class 'djstripe.models.payment_methods.DjstripePaymentMethod'>: not a StripeModel
Skipping <class 'djstripe.models.payment_methods.BankAccount'>: no stripe_class.list
Skipping <class 'djstripe.models.payment_methods.Card'>: no stripe_class.list
Skipping <class 'djstripe.models.payment_methods.Source'>: no stripe_class.list
Syncing PaymentMethod:
(no results)
Skipping <class 'djstripe.models.sigma.ScheduledQueryRun'>: only available in live mode
Skipping <class 'djstripe.models.webhooks.WebhookEventTrigger'>: not a StripeModel
Other Info: From the logs, it’s visible that djstripe_sync_models is trying to create both Prices and Customers from the account, but is failing to do so. I’ve investigated this further:
Shell with traceback here
>>> products = Product.api_list()
>>> products[0]
<Product product id=prod_IUgj5E7R49JavR at 0x7f469cabfae0> JSON: {
"active": true,
"attributes": [],
"created": 1606861771,
"description": "Up to 1,000 Users",
"id": "prod_IUgj5E7R49JavR",
"images": [],
"livemode": false,
"metadata": {},
"name": "Free Plan",
"object": "product",
"statement_descriptor": null,
"type": "service",
"unit_label": null,
"updated": 1607191828
}
>>> Product._get_or_create_from_stripe_object(products[0])
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 573, in get_or_create
return self.get(**kwargs), False
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 429, in get
raise self.model.DoesNotExist(
djstripe.models.api.APIKey.DoesNotExist: APIKey matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "djstripe_apikey_secret_key"
DETAIL: Key (secret)=(sk_test_omitted) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/djstripe/models/base.py", line 618, in _get_or_create_from_stripe_object
cls._create_from_stripe_object(
File "/usr/local/lib/python3.8/site-packages/djstripe/models/base.py", line 505, in _create_from_stripe_object
**cls._stripe_object_to_record(
File "/usr/local/lib/python3.8/site-packages/djstripe/models/base.py", line 332, in _stripe_object_to_record
owner_account = cls._find_owner_account(data)
File "/usr/local/lib/python3.8/site-packages/djstripe/models/base.py", line 259, in _find_owner_account
return Account.get_or_retrieve_for_api_key(api_key)
File "/usr/local/lib/python3.8/site-packages/djstripe/models/account.py", line 139, in get_or_retrieve_for_api_key
apikey_instance, _ = APIKey.objects.get_or_create_by_api_key(api_key)
File "/usr/local/lib/python3.8/site-packages/djstripe/models/api.py", line 40, in get_or_create_by_api_key
return super().get_or_create(
File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 576, in get_or_create
return self._create_object_from_params(kwargs, params)
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 610, in _create_object_from_params
obj = self.create(**params)
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.8/site-packages/djstripe/models/api.py", line 93, in save
return super().save(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 790, in save_base
updated = self._save_table(
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 933, in _do_insert
return manager._insert(
File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "djstripe_apikey_secret_key"
DETAIL: Key (secret)=(sk_test_omitted) already exists.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/djstripe/models/base.py", line 633, in _get_or_create_from_stripe_object
return cls.stripe_objects.get(id=id_), False
File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 429, in get
raise self.model.DoesNotExist(
djstripe.models.core.Product.DoesNotExist: Product matching query does not exist.
>>>
I was able to fetch actual data from my stripe account using Product.api_list()
but I was not able to manually save it to the database using Product._get_or_create_from_stripe_object()
because djstripe tried to resolve the api key from the database, could not find one, tried to create it, but it seemingly already existed.
I’ve tried specifying my keys directly in django admin and removed them from the environment, but this raises CRITICAL
errors on the startup check. This seems to contradict the docs which seem to indicate that either db keys or environmental keys may be used.
I then tried adding the same API key that was in the environment to the db manually through django admin, but this results in another unique constraint error even though there are no keys saved to the database.
Perhaps a regression in the move to db managed api keys?
Software versions
- Dj-Stripe version: 2.4.1
- Python version: 3.8.1
- Django version: 3.1.3
- Stripe API version: 2020-08-27
- Database type and version: Postgres 13
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:15 (8 by maintainers)
Top GitHub Comments
I just had this problem and solved it by getting rid of the logo and icon, running sync, and adding them back. This started happening on every event after upgrading to 2.4.1 from 2.3.0.
A work-around for this has landed and will be in 2.6.0.
Closed by 87b7cfcaff8cb7e9fdb94fadfd6a11815296f713.