I have a problem with Djang makemigrations / migrate tool
models.py
from django.contrib.auth.models import User
class Withdraw(models.Model):
employee_id = models.ForeignKey(Employee, on_delete = models.PROTECT)
amount = models.IntegerField(default=0)
withdraw_date = models.DateTimeField(default=timezone.now)
is_confirmed_by_employer = models.BooleanField(default=False)
I want to change it and have user as a foreignkey to user:
user = models.ForeignKey(User, on_delete=models.CASCADE)
I run makemigrations and I have this errormessage:
You are trying to add a non-nullable field 'user' to withdraw without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py
Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt
So Either way, Im not able to migrate this small change to the DB. Do you have any opinions? Thx!
Yes this is natural behavior.
What happened here is, When first you made other rows in database (earlier before making User field) User field was not there, and now when you are adding a User field in this database you are not setting it to null or blank so Django asks "what about previous saved rows? They can't be null." So best option was to fill those fields by providing one off default value. To do that you had to choose option 1 and then fill those missing spaces.
I do not recommend answer given by @jTiKey as if you allow users field to be blank or null, user could withdraw without giving in Users field and I think you'll not want that.