You are trying to add an invalid 'id' field to contact_info without default

I just use the simple python manage.py makemigrations command

However, all I get is an error:

You are trying to add a non-nullable field 'id' to contact_info 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) 2) Quit, and let me add a default in models.py 

Here are the .py models:

 class Document(models.Model): docfile = models.FileField(upload_to='documents/') class contact_number(models.Model): contact_numbers= models.CharField(max_length=13) class contact_address(models.Model): address = models.CharField(max_length=100) city = models.CharField(max_length=50) state = models.CharField(max_length=50) zip = models.CharField(max_length=5) class contact_info(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) email = models.EmailField() contact_numbers=models.ManyToManyField(contact_number) addresses=models.ManyToManyField(contact_address) 
+13
source share
8 answers

you can set `default =" "as well as editable = False.

For example, first_name = models.CharField(max_length=50, default="", editable=False) .

Adding an id field is not required. Django will add it automatically.

Edit: Deletes the latest migration files in your migration folder and retries again. If this does not work, repeat the same process, you will find that you deleted the desired file when your "makemigrations" command works.

+10
source

This happens when another field has been marked as primary key with primary_key=True earlier, and you delete it (in case django tries to add the id primary key).

Django requesting a default value for the primary key seems to be an error.

To work around this issue, follow these steps:

  1. Specify a random default value when prompted during makemigrations.

  2. Go to the created migration file (in the section your_app\migrations\ and delete default=x, your_app\migrations\ where x is the random value that you specified in step 1.

  3. While you are in the migration file, make sure that the procedure makes sense (for example, delete / change one primary key before adding another). Save and close.

  4. Migrate as usual.

+27
source

You need to set the default value. For instance:

 field_eg = models.CharField(default="") 

means:

 name = models.CharField(max_length=100, default="") 
+2
source

In my opinion, the complete destruction of migrations is a lazy and bad idea. I made snafu like @Afiz Momin and was able to dig myself out.

I had the following setup:

  1. Basic abstract model.
  2. Override id field after creation (bad idea)
  3. All other subclasses initiated Django migrations to try to create an ID field from the parent class.

So I got the following every time I ran makemigrations :

 You are trying to add a non-nullable field 'id' to <tablename> 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 

But there was a way out.

In my first attempt, I just ran a fake value and then deleted the id column created, but that was clearly unstable. But on a whim, I did the following:

  1. Backing up the migrations.orig directory to migrations.orig .
  2. Run ./manage.py makemigrations to complete all migrations.
  3. Check out the new migration file 0001-initial .

Most likely, the model she complains about ( <tablename> ) does not contain vital information. The bases argument was missing for me:

 migrations.CreateModel( name='<ModelName>', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('some_field', models.IntegerField()), # ... other fields ... ], options={ 'abstract': False, }, bases=(<base_classes>), ), 
  1. Now go back to migrations.orig and find the migration where the model is first created . The migration line will be similar to the one above.
  2. When you find the line in migrations.orig , change it to match the new migration code.
  3. Restore the original migrations. Remove migrations and rename migrations.orig to migrations .
  4. Just for good measure, run ./manage.py makemigrations and ./manage.py migrate . You should no longer receive error messages!
  5. If he complains about another model, follow the procedure described.
+1
source

This is because you have a non-empty database. There must be a line that was not created through Django ORM.

Do the following: python manage.py shell

 from <path_to_your_models> import * print len(list(contact_info.objects.filter(id = None))) 

This way you will know how many such lines are. If you want to save them, just do a script migration with some value that you give it to it.

0
source

I had a similar problem while doing makemigration. I had a primary key called "Identity" and other fields in my models to which I applied makemigrations. Later I changed the primary key name to "_id" and I got this error.

You are trying to add a non-zero "_id" field to exchange data without default; we cannot do this (the database needs to fill in the existing lines).

The error is misleading, but if you change "_id" to "Identity", you will not get this error.

How to fix? Remove the migration script generated in the migration batch. Or manually change the primary key in your generated migration script.

0
source

This can also happen if you make some changes to your .py models and try to migrate later. It happened to me once. In the end, deleting the entire database did not help. If an error occurs after adding the foreign key, you cannot specify a default value, or it may be unsafe to set the default value to null.

Django probably still has some of the previous migration files in your myapp / migrations / folder. It may help to delete these files.

0
source

Check your migration files

./manage.py showmigrations <App Name>

[X] 0001_initial

[X] 0002_auto_20181204_1110

Restore migration

./manage.py migrate <App Name> <migration file name> for example: 0001_initial or use zero (for full migration)

Check your migrations

./manage.py showmigrations <App Name>

[] 0001_initial

[] 0002_auto_20181204_1110

Now delete all return migrations and migrate again.

./manage.py migrate <App Name>

0
source

Source: https://habr.com/ru/post/1233156/


All Articles