Django: NOT NULL constraint failed
Я ставлю default значение в Foreign key, Django вроде видит это, но все равно добавляет NULL вместо default. Я пытался убирать null=true/blank=True, но не помогает. Вот sql и models.py:
SQl
[{'sql': 'SELECT "first_app_director"."id", "first_app_director"."first_name", "first_app_director"."bio" FROM "first_app_director"
WHERE "first_app_director"."first_name" = \'No director\' LIMIT 21', 'time': '0.000'},
{'sql': 'INSERT INTO "first_app_film" ("name", "director_id") VALUES (\'Reservoir Dogs\', NULL) RETURNING "first_app_film"."id"', 'time': '0.016'}]
Models.py
from django.db import models
# Create your models here.
class Director(models.Model):
first_name = models.CharField(max_length=150)
bio = models.TextField()
@classmethod
def get_default(cls):
obj, created = cls.objects.get_or_create(first_name='No director')
def __str__(self) -> str:
return str(self.first_name)
class Film(models.Model):
name = models.CharField(max_length=150)
director = models.ForeignKey(Director,
on_delete=models.SET_DEFAULT,
default=Director.get_default,
related_name='director',
null=True, blank=True
)
def __str__(self) -> str:
return f'"{self.name}" - {self.director}' if self.director else self.name
Ошибка:
**>>> film = Film.objects.create(name='Reservoir Dogs')**
Traceback (most recent call last):
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: first_app_film.director_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 671, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 831, in save
self.save_base(
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 882, in save_base
updated = self._save_table(
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 1025, in _save_table
results = self._do_insert(
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 1066, in _do_insert
return manager._insert(
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1790, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1657, in execute_sql
cursor.execute(sql, params)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 103, in execute
return super().execute(sql, params)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\МС\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: first_app_film.director_id