Repository: dj-stripe/dj-stripe

Issue:
django 4.0.0 missing migrations
**Describe the bug**
1. Install django >= 4.0 and latest djstripe
2. `./manage.py migrate`
```
  Your models in app(s): 'djstripe' have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
```

No such warning is generated with django < 4.0


Discussion:
I've the same issue. If I try to run `manage.py makemigrations` it reports:

```
Migrations for 'djstripe':
  /usr/local/lib/python3.10/site-packages/djstripe/migrations/0011_alter_invoice_charge_alter_invoice_customer_and_more.py
    - Alter field charge on invoice
    - Alter field customer on invoice
    - Alter field default_tax_rates on invoice
    - Alter field subscription on invoice
    - Alter field tax_rates on invoiceitem
    - Alter field default_tax_rates on subscription
    - Alter field tax_rates on subscriptionitem
    - Alter field charge on upcominginvoice
    - Alter field customer on upcominginvoice
    - Alter field subscription on upcominginvoice
Traceback (most recent call last):
  File "/app/manage.py", line 31, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 417, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 90, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/makemigrations.py", line 190, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/makemigrations.py", line 228, in write_migration_files
    with open(writer.path, "w", encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.10/site-packages/djstripe/migrations/0011_alter_invoice_charge_alter_invoice_customer_and_more.py'
```
@edomora97 @ayushin It seems this is a `side-effect` of `Django 4.0` and not specific to `dj-stripe`. Please check this [link](https://docs.djangoproject.com/en/4.0/releases/4.0/#migrations-autodetector-changes)
@arnav13081994 we will probably have to regenerate migrations for 4.0, I'll investigate as soon as I can.

Resolving patch:
diff --git a/djstripe/migrations/0001_initial.py b/djstripe/migrations/0001_initial.py
--- a/djstripe/migrations/0001_initial.py
+++ b/djstripe/migrations/0001_initial.py
@@ -1603,7 +1603,7 @@ class Migration(migrations.Migration):
                         help_text="The latest charge generated for this invoice, if any.",
                         null=True,
                         on_delete=django.db.models.deletion.CASCADE,
-                        related_name="latest_invoice",
+                        related_name="latest_%(class)s",
                         to="djstripe.charge",
                     ),
                 ),
@@ -1611,7 +1611,7 @@ class Migration(migrations.Migration):
                     "customer",
                     djstripe.fields.StripeForeignKey(
                         on_delete=django.db.models.deletion.CASCADE,
-                        related_name="invoices",
+                        related_name="%(class)ss",
                         to="djstripe.customer",
                         to_field=settings.DJSTRIPE_FOREIGN_KEY_TO_FIELD,
                     ),
@@ -1621,7 +1621,7 @@ class Migration(migrations.Migration):
                     djstripe.fields.StripeForeignKey(
                         null=True,
                         on_delete=django.db.models.deletion.SET_NULL,
-                        related_name="invoices",
+                        related_name="%(class)ss",
                         to="djstripe.subscription",
                         to_field=settings.DJSTRIPE_FOREIGN_KEY_TO_FIELD,
                     ),
@@ -2714,7 +2714,7 @@ class Migration(migrations.Migration):
                         help_text="The latest charge generated for this invoice, if any.",
                         null=True,
                         on_delete=django.db.models.deletion.CASCADE,
-                        related_name="latest_upcominginvoice",
+                        related_name="latest_%(class)s",
                         to="djstripe.charge",
                     ),
                 ),
@@ -2722,7 +2722,7 @@ class Migration(migrations.Migration):
                     "customer",
                     djstripe.fields.StripeForeignKey(
                         on_delete=django.db.models.deletion.CASCADE,
-                        related_name="upcominginvoices",
+                        related_name="%(class)ss",
                         to="djstripe.customer",
                         to_field=settings.DJSTRIPE_FOREIGN_KEY_TO_FIELD,
                     ),
@@ -2752,7 +2752,7 @@ class Migration(migrations.Migration):
                     djstripe.fields.StripeForeignKey(
                         null=True,
                         on_delete=django.db.models.deletion.SET_NULL,
-                        related_name="upcominginvoices",
+                        related_name="%(class)ss",
                         to="djstripe.subscription",
                         to_field=settings.DJSTRIPE_FOREIGN_KEY_TO_FIELD,
                     ),
@@ -2880,7 +2880,7 @@ class Migration(migrations.Migration):
                 blank=True,
                 db_table="djstripe_djstripeinvoicedefaulttaxrate",
                 help_text="The tax rates applied to this invoice, if any.",
-                related_name="_invoice_default_tax_rates_+",
+                related_name="+",
                 to="djstripe.TaxRate",
             ),
         ),
@@ -2891,7 +2891,7 @@ class Migration(migrations.Migration):
                 blank=True,
                 db_table="djstripe_djstripesubscriptiondefaulttaxrate",
                 help_text="The tax rates that will apply to any subscription item that does not have tax_rates set. Invoices created will have their default_tax_rates populated from the subscription.",
-                related_name="_subscription_default_tax_rates_+",
+                related_name="+",
                 to="djstripe.TaxRate",
             ),
         ),
@@ -3822,7 +3822,7 @@ class Migration(migrations.Migration):
                         blank=True,
                         db_table="djstripe_djstripesubscriptionitemtaxrate",
                         help_text="The tax rates which apply to this subscription_item. When set, the default_tax_rates on the subscription do not apply to this subscription_item.",
-                        related_name="_subscriptionitem_tax_rates_+",
+                        related_name="+",
                         to="djstripe.TaxRate",
                     ),
                 ),
@@ -4272,7 +4272,7 @@ class Migration(migrations.Migration):
                         blank=True,
                         db_table="djstripe_djstripeinvoiceitemtaxrate",
                         help_text="The tax rates which apply to this invoice item. When set, the default_tax_rates on the invoice do not apply to this invoice item.",
-                        related_name="_invoiceitem_tax_rates_+",
+                        related_name="+",
                         to="djstripe.TaxRate",
                     ),
                 ),


Test patch: