'django.db.utils.IntegrityError: The row in table 'main_page_projects' with primary key '1' has an invalid foreign key
Created first project model and populated it with some data, then tried to add profile model to it (no data there) through foreign key and while trying to do a migration for linking profile model with projects model getting an error:
django.db.utils.IntegrityError: The row in table 'main_page_projects' with primary key '1' has an invalid foreign key: main_page_projects.profile_id contains a value '1' that does not have a corresponding value in main_page_profile.id.
Value nr 1 was picked during the makemigration:
It is impossible to add a non-nullable field 'profile' to projects without specifying a default. This is because 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 manually define a default value in models.py.
I googled around and looked also stackoverflow, probably if I delete migration files and sqlite3 database then migrations should work, but I would like to know how to make it to work without deleting migration files and database. If populate profile then migration also works, but why it won't create a link with default values?
What should I do differently or where I go wrong?
Using Django 4.0.2 and Python 3.9.5
models.py
from django.db import models
class Profile(models.Model):
full_name = models.CharField(max_length=50, verbose_name='Name')
email = models.EmailField(unique=True, verbose_name='Email')
bio = models.TextField(max_length=500, verbose_name='Description')
profile_picture = models.ImageField(upload_to='profile_picture')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Profile'
verbose_name_plural = 'Profiles'
class Projects(models.Model):
name = models.CharField(max_length=50, unique=True, verbose_name='Name')
slug = models.SlugField(unique=True)
tech = models.CharField(max_length=50)
description = models.TextField(max_length=500, verbose_name='Description')
image = models.ImageField(upload_to='portfolio/')
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Project'
verbose_name_plural = 'Projects'
Projects migration and Profile migration code
Projects migration
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Projects',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50, unique=True, verbose_name='Name')),
('slug', models.SlugField(unique=True)),
('tech', models.CharField(max_length=50)),
('description', models.TextField(max_length=150, verbose_name='Description')),
('image', models.ImageField(upload_to='portfolio/')),
],
),
]
Profile migration
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('main_page', '0003_projects_image'),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('full_name', models.CharField(max_length=50, verbose_name='Name')),
('email', models.EmailField(max_length=254, unique=True, verbose_name='Email')),
('bio', models.TextField(max_length=500, verbose_name='Description')),
('profile_picture', models.ImageField(upload_to='profile_picture')),
],
options={
'verbose_name': 'Profile',
'verbose_name_plural': 'Profiles',
},
),
]
Foreign key relation migration
# Generated by Django 4.0.2 on 2022-02-11 08:05
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('main_page', '0004_profile'),
]
operations = [
migrations.AddField(
model_name='projects',
name='profile',
field=models.ForeignKey(default='1', on_delete=django.db.models.deletion.CASCADE, to='main_page.profile'),
preserve_default=False,
),
]
Solution 1:[1]
You can not create an entry in a database table (or modify one by adding a field via migrations) with a ForeignKey that points to a non existing entry on the target table ("Profile" in your case). It does not make sense - so you get the integrity error. Leave away the default=1 and make it "blank=True, null=True" so you can leave it empty upon creation or during migration.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Razenstein |
