0
Django automatically creates an Autofield for primary key if there is no field with primary_key=True, so I’m trying to remove the primary key I created (identifier field in the Client model) and leave the default key provided by Django.
class Cliente(models.Model):
identificador = models.AutoField(
'id',
default=None,
primary_key=True
)
class Consulta(models.Model):
cliente = models.ForeignKey(
Cliente,
on_delete=models.CASCADE,
default=None,
unique=True,
)
But when I execute python Manage.py migrate get that mistake:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\panto\anaconda3\lib\site-packages\django\core\management\commands\migrate.py", line 245, in handle
fake_initial=fake_initial,
File "C:\Users\panto\anaconda3\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\panto\anaconda3\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\panto\anaconda3\lib\site-packages\django\db\migrations\executor.py", line 229, in apply_migration
migration_recorded = True
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py", line 35, in __exit__
self.connection.check_constraints()
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 324, in check_constraints
violations = cursor.execute('PRAGMA foreign_key_check').fetchall()
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\panto\anaconda3\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\panto\anaconda3\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "C:\Users\panto\anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 411, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: foreign key mismatch - "sitepsi_consulta" referencing "sitepsi_cliente"
Rename identifier for id also leads to the same error. I have tried creating a new Migrations folder, use python Manage.py flush and python Manage.py <app_name> zero.