question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

django-getpaid won't migarte in new project

See original GitHub issue
  • django-getpaid version: 2.2.2
  • Django version: 3.2.3
  • Python version: 3.8
  • Operating System: macOS

Description

As I’ve created new project and I wanted to use django-getpaid I’ve just installed it with poetry, defined an Order instance and tried to migrate the app.

Result:

$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py", line 172, in handle
    changes = autodetector.changes(
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/autodetector.py", line 41, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/autodetector.py", line 126, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 212, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 277, in __init__
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field getpaid.Payment.order was declared with a lazy reference to 'backend.order', but app 'backend' doesn't provide model 'order'.

Definition of an Order model:

class Order(AbstractOrder):
    items = models.ManyToManyField("Product", related_name='items', through="OrderItem")
    description = models.CharField(max_length=128)
    buyer_email = models.EmailField()
    buyer_phone = models.CharField(max_length=12)

    def is_ready_for_payment(self):
        pass

    def get_total_amount(self):
        final = Decimal(0)

        for order_item in self.items.all():
            final += order_item.item.final_price_with_tax * order_item.amount
        return final

    def get_buyer_info(self):
        return {"email": self.buyer_email, "phone": self.buyer_phone}

    def get_currency(self):
        return "PLN"

    def get_description(self):
        return self.description

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'getpaid',
    'getpaid_payu',  # one of plugins
    'rest_framework',
    'backend.apps.BackendConfig',
    'corsheaders',
]
GETPAID_ORDER_MODEL = 'backend.Order'
GETPAID_BACKEND_SETTINGS = {
    "getpaid_payu": {
        # take these from your merchant panel:
        "pos_id": os.getenv('PAYU_POS_ID'),
        "second_key": os.getenv("PAYU_SECOND_KEY"),
        "oauth_id": os.getenv('PAYU_OAUTH_ID'),
        "oauth_secret": os.getenv("PAYU_OAUTH_SECRET"),
    },
}

What I Did

Recreate an database and migrations

Result: Fail, django-getpaid requires at least one migration to run. Traceback

$ python manage.py migrate backend 
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/loader.py", line 259, in build_graph
    self.graph.validate_consistency()
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/graph.py", line 195, in validate_consistency
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/graph.py", line 195, in <listcomp>
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/Users/mateusz/Library/Caches/pypoetry/virtualenvs/drop-OdaMbioT-py3.8/lib/python3.8/site-packages/django/db/migrations/graph.py", line 58, in raise_error
    raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration getpaid.0001_initial dependencies reference nonexistent parent node ('backend', '0001_initial')

Core code modifications

Result: success, but after following steps:

  1. I’ve commented these lines in getpaid/models.py

class Payment(AbstractPayment):
    class Meta(AbstractPayment.Meta):
        swappable = swapper.swappable_setting("getpaid", "Payment")

after what I’ve received

RuntimeError: Model class getpaid.models.Payment doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

so I’ve commented get_paid in INSTALLED_APPS

  1. Migrate my app once, so I have an Order in my schema
  2. Uncomment the Payment model in the app and getpaid in INSTALLED_APPS
  3. Migrated it once again.

Summary

Actually I’m not sure if I did not broke something, I’ll update you guys after some tests, but anyway, did I do something wrong? I actually followed the documentation, so If it’s my fault maybe docs update should be considered?

Have great day, Mat.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dekozacommented, Jun 12, 2021

OK, I’ll use your test_project to investigate it further. Thanks!

1reaction
rythm-of-the-red-mancommented, Jun 12, 2021

Hi @dekoza thanks for blazing fast response! The thing is that even with initial migration it does not goes through. Check this example repo: https://github.com/BuSSioR/getpaid_issue_example/ After python manage.py migrate it throws:

$ python manage.py migrate
SystemCheckError: System check identified some issues:

ERRORS:
getpaid.Payment.order: (fields.E300) Field defines a relation with model 'test_project.Order', which is either not installed, or is abstract.
getpaid.Payment.order: (fields.E307) The field getpaid.Payment.order was declared with a lazy reference to 'test_project.order', but app 'test_project' doesn't provide model 'order'.

and show_migrations after I comment get_paid from INSTALLED APPS and MyCustomOrder shows that every other migration is in place

$ python manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
test_project
 [X] 0001_initial
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Django 1.7 - "No migrations to apply" when run ...
"This will make a new initial migration for your app. Now, when you run migrate, Django will detect that you have an initial...
Read more >
Migrations - Django documentation
Migrations¶. Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database...
Read more >
How to solve broken migration history in Django - Dev Genius
If some migrations of that app were deleted and the history of the migrations was broken, one solution is to clear the migration...
Read more >
How to Move a Django Model to Another App - Real Python
There are several ways to move a Django model from one app to another using Django migrations, but unfortunately none of them are...
Read more >
makemigration and migrate doesn't work docker production
Your models have changes that are not yet reflected in a migration, and so won't be applied. Run "manage.py makemigrations" to make new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found