Missing fields after migration in Django project

I am interested in learning python and Django and I've started this course. I am having difficulties in migrating the models in the project. Following the course, on chapter "Models".

I've inserted the given code in the boards/models.py file as instructed. The code:

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

class Board(models.Model):
    name = models.CharField(max_length=30, unique=True)
    description = models.CharField(max_length=100)

class Topic(models.Model):
    subject = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board, related_name='topics')
    starter = models.ForeignKey(User, related_name='topics')

class Post(models.Model):
    message = models.TextField(max_length=4000)
    topic = models.ForeignKey(Topic, related_name='posts')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(User, related_name='posts')
    updated_by = models.ForeignKey(User, null=True, related_name='+')`

Following the instructions to the "Migrating the Models" chapter and running this command: "python manage.py makemigrations" I should receive the following message:

Migrations for 'boards': boards/migrations/0001_initial.py - Create model Board - Create model Post - Create model Topic - Add field topic to post - Add field updated_by to post

Instead, I get the following errors:

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 "D:\Desktop\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 419, in     execute_from_command_line
    utility.execute()
  File "D:\Desktop\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 395, in     execute
    django.setup()
  File "D:\Desktop\myproject\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Desktop\myproject\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "D:\Desktop\myproject\venv\lib\site-packages\django\apps\config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Zinkov\AppData\Local\Programs\Python37\lib\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 "D:\Desktop\myproject\myproject\boards\models.py", line 8, in <module>
    class Topic(models.Model):
  File "D:\Desktop\myproject\myproject\boards\models.py", line 11, in Topic
    board = models.ForeignKey(Board, related_name='topics')
TypeError: __init__() missing 1 required positional argument: 'on_delete'`

I've added the missing argument "on_delete" on the following lines:

board = models.ForeignKey(Board, related_name='topics', on_delete=models.CASCADE)
starter = models.ForeignKey(User, related_name='topics', on_delete=models.CASCADE)

topic = models.ForeignKey(Topic, related_name='posts',max_length=256, on_delete=models.CASCADE)
created_by = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, null=True, related_name='+',max_length=256,         on_delete=models.CASCADE)`

After running the "makemigrations" command again the migration was successful but without adding the fields as expected. My output is:

Migrations for 'boards': boards\migrations\0001_initial.py - Create model Board - Create model Topic - Create model Post

Instead of:

Migrations for 'boards': boards\migrations\0001_initial.py - Create model Board - Create model Topic - Create model Post - Add field topic to post - Add field updated_by to post

I am sorry for the long post. Thank you!

While creating model where you have used ForeignKey relation it accepts on_delete argument and that is missing. Adding it should work. Kindly refer to below code

class Topic(models.Model):
     subject = models.CharField(max_length=255)
     last_updated = models.DateTimeField(auto_now_add=True)
     board = models.ForeignKey(Board, related_name='topics', on_delete=models.SET_NULL)
     starter = models.ForeignKey(User, related_name='topics', on_delete=models.SET_NULL)

You can refer to this link for more on_delete arguments [https://www.geeksforgeeks.org/python-relational-fields-in-django-models/]

Back to Top