wandermonk
wandermonk

Reputation: 7346

Having trouble running migrations using django 2.7.1

I am very new to Django. I have been following some tutorials to learn the framework.

I am using django in the virtualenv. When i run the command

python manage.py makemigrations

I am getting the below error

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 5, in <module>
    class Game(models.Model):
  File "/Users/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 7, in Game
    related_name="games_first_player")
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Here is the model class

from django.db import models
from django.contrib.auth.models import  User


class Game(models.Model):
    first_player = models.ForeignKey(User,
                                     related_name="games_first_player")
    second_player = models.ForeignKey(User,
                                      related_name="games_second_player")

    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)

class Move(models.Model):
    x = models.IntegerField()
    y = models.IntegerField()
    comment = models.CharField(max_length=300, blank=True)
    by_first_player = models.BooleanField()

    game = models.ForeignKey(Game, on_delete=models.CASCADE)

Appreciate your help in understanding this issue and fixing this.

Upvotes: 0

Views: 44

Answers (2)

Shubham Rath
Shubham Rath

Reputation: 68

Since Django 2.0 the ForeignKey field requires two positional arguments:

  1. the model to map to
  2. the on_delete argument

Read the documentation and add on_delete accordingly to the below.

first_player = models.ForeignKey(User,
                                related_name="games_first_player")
second_player = models.ForeignKey(User,
                                related_name="games_second_player")

Upvotes: 2

mfrackowiak
mfrackowiak

Reputation: 1304

Starting from Django 2.0, the on_delete parameter is mandatory for all ForeignKey fields. You need to change your first and second player:

class Game(models.Model):
    first_player = models.ForeignKey(
        User, related_name="games_first_player", on_delete=models.CASCADE
    )
    second_player = models.ForeignKey(
        User, related_name="games_second_player", on_delete=models.CASCADE
    )

    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)

Upvotes: 2

Related Questions