A
A
AliminVerckon2020-07-22 15:06:17
Django
AliminVerckon, 2020-07-22 15:06:17

Django throws an error (something related to the database) what to do?

class Products(models.Model):
  name = models.CharField("Имя продукта", max_length = 150)
  img = models.ImageField("Изображение", upload_to = "product/")
  description = models.TextField("Описание")
  cost = models.IntegerField("цена", default = 0)
  in_stock = models.BooleanField("наличие", default=False)
  hot = models.BooleanField("Хит", default = False)
  sale = models.BooleanField("Скидка", default=False)
  kg = models.BooleanField("В кг", default=True)
  litr = models.BooleanField("В литрах", default=False)
  draft = models.BooleanField("черновик", default=False)
  url = models.SlugField(max_length=160, unique = True)

  def __str__(self):
    return {self.name}

  class Meta:
    verbose_name = 'Продукт'
    verbose_name_plural = "Продукты"


class Products_tip(models.Model):
  name = models.CharField("Имя типа продукта", max_length = 150)
  id_tip = models.CharField("ID типа", max_length=160, unique=True)
  foregin = models.ForeignKey(Products, verbose_name='Тип товара', null=True, on_delete=models.SET_NULL)

  def __str__(self):
    return f'{self.name} - {self.id_tip}'

  class Meta:
    verbose_name='Тип'
    verbose_name_plural = "Типы"


Error:
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, flatpages, sessions, sites
Running migrations:
Applying app.0014_auto_20200722_1428...Traceback (most recent call last):
File "C:\Users\Zver\ venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Zver\venv\lib\site- packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: new__app_products.tip_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 21, in
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Zver\venv\lib\site-packages\django \core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Zver\venv\lib\site-packages\django\core\management\__init__.py", line 395 , in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Zver\venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self .execute(*args, **cmd_options)
File "C:\Users\Zver\venv\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "C:\Users\Zver\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func( *args, **kwargs)
File "C:\Users\Zver\venv\lib\site-packages\django\core\management\commands\migrate.py", line 233, in handle
fake_initial=fake_initial,
File "C: \Users\Zver\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Zver\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,fake_initial=fake_initial)
File "C:\Users\Zver\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\ Zver\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Zver\venv \lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
field,
File "C:\Users\Zver\venv\lib\site-packages\django\db\backends\ sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\Zver\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 287, in _remake_table
self.quote_name(model._meta.db_table),
File "C: \Users\Zver\venv\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "C:\Users\Zver\venv\ lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Zver\venv\lib\site-packages\ django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Zver\venv\lib\site- packages\django\db\backends\utils.py", line 77,in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Zver\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute (sql, params)
File "C:\Users\Zver\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\ Users\Zver\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Zver\venv\ lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: new__app_products.tip_id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-07-22
@AliminVerckon

learn and repair
NOT NULL constraint failed
means when adding an object, you did not set a non-zero value,
or in the code, set it or in the model, set that is_null = True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question