Django: non-nullable field without a default - python

In Django I use manage.py makemigrations and manage.py migrate on the following models.py file:
class Family(models.Model):
comment1 = models.CharField(max_length=80)
#comment2 = models.CharField(max_length=80)
After this successful initialization, I changed models.py to (I just uncomment the new model field which is basically a copy of the other model field):
class Family(models.Model):
comment1 = models.CharField(max_length=80)
comment2 = models.CharField(max_length=80)
Now when I try to makemigrations again I get the following error:
You are trying to add a non-nullable field 'comment' to family 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:
Why didn't I get this error upon intialization in the first place?

Others are right. you should set a default value for that field.
but there is a a trick that you can solve this. but it is not a good way... only if you have no choice.
1. comment all of your table
2. run makemigrations and migrate
3. uncomment your table
4.2. run makemigrations and migrate again

image1=models.ImageField(upload_to='app/image12',help_text="hi", null=True)
That is, set null= True'in the field.
It happens when you change your model after stored database.

If you want to make it non nullable without default.
you have to provide value for the same field when you create migration file. thats why it asks for the option. type "1" in terminal while python manage.py makemigrations and then provide a value for the field for previously saved row(if any).
Let me know if this helps.

I have faced a same issue, in my case:
Focus on the alert:
...non-nullable field 'comment' to family...
model: Family
field: comment
Just add one more attr in the field comment of the model family:
default=''

You can add this: default=None or default="something"
or add null=True, blank=True.

Related

I'm getting an error message when making migration to db in django

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.
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:
Provide the default by hand, and the django it's going to replace all the null values of that field with this
Set a default value in the model, example:
number = models.IntegerField(default=1)
string = models.CharField(default='')
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

How can i set a default value to an OneToOneField relationship which it's automatically generated?

I have two model classes they like below:
from django.db import models
from django.urls import reverse
class Place(models.Model):
address = models.CharField(max_length=80)
class Author(Place):
name = models.CharField(max_length=50)
But when i want to execute makemigartions django shows me error below:
You are trying to add a non-nullable field 'place_ptr' to author 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:
I have some data in Author table but i dont want to remove that data.
is there anyone could help me please ?
You can delete the old migrations file or entire migration folder for the specific app and then do the python manage.py makemigrations <app_name>
If this is not the solution you wanted then you can find the solution here.

How to remove a non-nullable field in Django?

I've got a category field in my model which I want to remove. However it did not have null=True so (from previous experience) deleting it will cause a django.db.utils.IntegrityError: null value in column “category” violates not-null constraint error.
Any way around this?
Here's is the field:
class Post(models.Model):
...
category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='1')
You must be run the manage.py makemigrations
And manage.py migrate to apply the changes
By default when you create a new django field and if you don't provide the argument null explicitly, it will be treated as False, so if you don't specified null argument at the creation it will be by default null = False. Just delete the field,save the file and make the python.manage.py makemigrations and then python.manage.py migrate to reflect the changes you have made to your model in the DB

Django datetime default value in migrations

I've read some questions about this issue in so, also this question is not giving the correct answer for my case:
I'm adding a created_time field in my already existing models, so no date in the mysql table belonging to the model.
class Configs(models.Model):
...
creation_date = models.DateTimeField(auto_now_add=True, blank=True)
...
I apply the migration using python manage.py makemigrations
And I get this error:
You are trying to add a non-nullable field 'creation_date' to
collections without a default; we can't do that (the database needs
something to populate existing rows). Please select a fix:
I tryed many options:
creation_date = models.DateTimeField(auto_now_add=True)
This one is giving the same error.
How can this migration be achieved, if USE_TZ in settings is set to False?
Btw, is this a bug in Django 1.9.4 makemigrations script?
auto_now_add set the current datetime when the instance is created, which never happens during your migration so you are trying to leave NULL a non-nullable field.
The solution would be to add a default date to your model, makemigrations, then remove the default parameter from the model, makemigrations again and finally migrate. You can achieve the same adding null=True to the field, adding a RunPython or RunSQL to your migration that populates the field, and then remove the null=true from the field.
At the end you can merge both migration files (or simple write it yourself) to end with something like:
operations = [
migrations.AddField(
model_name='configs',
name='creation_date',
field=models.DateTimeField(auto_now_add=True, null=True, blank=True),
),
migrations.RunPython(populate_dates),
migrations.AlterField(
model_name='configs',
name='creation_date',
field=models.DateTimeField(auto_now_add=True, blank=True),
),
]
I just wrote it so I hope it does not have many typos

'NOT NULL constraint failed' after adding to models.py

I'm using userena and after adding the following line to my models.py
zipcode = models.IntegerField(_('zipcode'),
max_length=5)
I get the following error after I hit the submit button on th signup form:
IntegrityError at /accounts/signup/
NOT NULL constraint failed: accounts_myprofile.zipcode
My question is what does this error mean, and is this related to Userena?
You must create a migration, where you will specify default value for a new field, since you don't want it to be null. If null is not required, simply add null=True and create and run migration.
coldmind's answer is correct but lacks details.
The NOT NULL constraint failed occurs when something tries to set None to the zipcode property, while it has not been explicitly allowed.
It usually happens when:
Your field has Null=False by default, so that the value in the database cannot be None (i.e. undefined) when the object is created and saved in the database (this happens after a objects_set.create() call or setting the .zipcode property and doing a .save() call).
For instance, if somewhere in your code an assignment results in:
model.zipcode = None
This error is raised.
When creating or updating the database, Django is constrained to find a default value to fill the field, because Null=False by default. It does not find any because you haven't defined any. So this error can not only happen during code execution but also when creating the database?
Note that the same error would be returned if you define default=None, or if your default value with an incorrect type, for instance default='00000' instead of 00000 for your field (maybe can there be an automatic conversion between char and integers, but I would advise against relying on it. Besides, explicit is better than implicit). Most likely an error would also be raised if the default value violates the max_length property, e.g. 123456
So you'll have to define the field by one of the following:
models.IntegerField(_('zipcode'), max_length=5, Null=True,
blank=True)
models.IntegerField(_('zipcode'), max_length=5, Null=False,
blank=True, default=00000)
models.IntegerField(_('zipcode'), max_length=5, blank=True,
default=00000)
and then make a migration (python3 manage.py makemigration <app_name>) and then migrate (python3 manage.py migrate).
For safety you can also delete the last failed migration files in <app_name>/migrations/, there are usually named after this pattern:
<NUMBER>_auto_<DATE>_<HOUR>.py
Finally, if you don't set Null=True, make sure that mode.zipcode = None is never done anywhere.
If the zipcode field is not a required field then add null=True and blank=True, then run makemigrations and migrate command to successfully reflect the changes in the database.
In Django Null=True means Null Values are accepted. But Some Filed having Django that Blank=True are not satisfied for put Blank fields.
DateTimeField
ForeignKey
These two fields are used and if you want to put Blank I recommend adding NULL=TRUE
Since you added a new property to the model, you must first delete the database. Then manage.py migrations then manage.py migrate.

Categories