1

hello guys I'm doing a django tutorial and i missed a change the instructor did in models.py so i fix it but when trying to make the migration to the db it gives me a code that I don't understand or i don't know what to do, here is what it says:

(tonyto) PS E:\web-dev\Python\Django1\myappito> python manage.py makemigrations

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

they way it was name: models.CharField(max_length=100) details: models.CharField(max_length=500)

and this is how i update it name: models.CharField(max_length=100) details: models.CharField(max_length=500) thank you in advance.

3
  • Press 1 from the keyboard. Django will replace all the existing rows with a null values. If you find this answer, helpful please mark it as the solution. Commented Nov 8, 2021 at 17:44
  • it will be helpful to share new field you have added which casing the migration Commented Nov 8, 2021 at 17:51
  • my code was this way name: models.CharField(max_length=100) details: models.CharField(max_length=500) and now is like this name = models.CharField(max_length=100) details = models.CharField(max_length=500) Commented Nov 8, 2021 at 18:31

2 Answers 2

1

first approach

  • this comes because you run the migration on each field created so the previous created filled doesn't accept null value so you want to provide default value in your case it is string value
  • you can choose the first choice and but the answer to be "default" with a quotation to understand it as a string

The second solution

delete the latest file created in the migration folder and modify your model to be

'''
name = models.CharField(max_length=100,null=True) 
details = models.CharField(max_length=500, null=True) 
'''

then run the migration again

python manage.py makemigration python manage.py migrate then go back to model again and remove null from each field and run python manage.py makemigration python manage.py migrate

Sign up to request clarification or add additional context in comments.

Comments

0

This was caused because you're trying to migrate a model field that cannot be null, but since it cannot be null, it needs a default value so that django can replace all the existing rows with the null value of that field.

You have two options:

  1. Provide the default by hand, and the django it's going to replace all the null values of that field with this

  2. Set a default value in the model, example:

number = models.IntegerField(default=1)

string = models.CharField(default='')

3 Comments

yes that's what I tried but is not budging and I dont get what values is asking for i tried option one and two
this is correct answer but you need to remove the last file create by makemigration and run makemigration again after adding this answer
ok cool so if i get it right , what you mean is to go to the folder and delete the file made by makemigrations and then do the process all over again with the updated values ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.