3
respostas

migração

antes de me deparar com este erro eu me deparei com o outro que acabei resolvendo com o stackoverflow aviso do problema:

WARNINGS: receitas.Receita: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the ReceitasConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

solução do stackoverflow:

class Receita(models.Model):
    id = models.AutoField(primary_key=True)
    pessoa = models.ForeignKey(Pessoa, on_delete=models.CASCADE)
    nome_receita = models.CharField(max_length=200)
    ingredientes = models.TextField()
    modo_preparo = models.TextField()
    tempo_preparo = models.IntegerField()
    rendimento = models.CharField(max_length=100)
    categoria = models.CharField(max_length=100)
    date_receita = models.DateTimeField(default=datetime.now, blank=True)

feito a migração com sucesso e o WARNINGS sumiu

coloquei essas coisas acima pq pensei que poderia ter relação ao que aconteceu dps

Adicionei o atributo pessoa à classe Receita, rodei o makemigrations e ao rodar o migrate recebi a seguinte mensagem mais ou menos assim:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pessoas, receitas, sessions
Running migrations:
  Applying receitas.0003_receita_pessoa...Traceback (most recent call last):
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: column "pessoa_id" contains null values


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

Traceback (most recent call last):

............. um monte de file que nao vai caber aqui no post.........................

django.db.utils.IntegrityError: column "pessoa_id" contains null values
3 respostas

eae Gabriel !

Faz um teste ai, atribua um valor Default para a FK de pessoa_id

pessoa = models.ForeignKey(Pessoa, on_delete=models.CASCADE, default='')

Acredito que isso deve resolver.

até +

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pessoas, receitas, sessions
Running migrations:
  Applying receitas.0003_receita_pessoa...Traceback (most recent call last):
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: column "pessoa_id" contains null values


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

Traceback (most recent call last):
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\manage.py", line 22, in <module> 
    main()
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\manage.py", line 18, in main     
    execute_from_command_line(sys.argv)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)

CONTINUAÇÃO

  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\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\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\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\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\base\schema.py", line 522, in add_field
    self.execute(sql, params)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\base\schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\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\Usuario\Documents\ESTUDOS\CURSOS\CURSOS\ALURA\cadernos\python\16. Django\1. Django-app_receitas\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "pessoa_id" contains null values