I added a custom extension to djangos User model and now I'm getting this error on my localhost url:
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column users_account.birthday does not exist
LINE 1: ... "users_account"."id", "users_account"."user_id", "users_acc...
^
Furthermore, when I tried to migrate my changes I got this error in the terminal:
Operations to perform:
Apply all migrations: admin, auth, chaburah, contenttypes, sessions, taggit, users
Running migrations:
Applying users.0002_alter_account_birthday...Traceback (most recent call last):
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "birthday" does not exist
LINE 1: ..._account" ALTER COLUMN "birthday" TYPE date USING "birthday"...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/almoni/Desktop/Code/my_chaburah/manage.py", line 22, in <module>
main()
File "/Users/almoni/Desktop/Code/my_chaburah/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute
output = self.handle(*args, **options)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/migration.py", line 131, in apply
operation.database_forwards(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 747, in alter_field
self._alter_field(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 231, in _alter_field
super()._alter_field(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 963, in _alter_field
self.execute(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 192, in execute
cursor.execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 103, in execute
return super().execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "birthday" does not exist
LINE 1: ..._account" ALTER COLUMN "birthday" TYPE date USING "birthday"...
It only appears when I try to either edit an existing User or create a new one.
models.py:
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.DateTimeField(blank=True, null=True)
def __str__(self):
return self.user
admin.py:
class AccountInline(admin.StackedInline):
model = Account
can_delete = False
verbose_name_plural = 'Accounts'
class CustomUserAdmin(UserAdmin):
inlines = (AccountInline,)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
My original guess was the error was due to the fact that my existing Users have no birthday but that doesn't explain why I can't create a new User. Which makes me think I am unaware of what the actual problem is.
I'm newish to django/SQl so I don't really understand the error itself.
You forgot about:
python manage.py makemigrations
# and/or
python manage.py migrate
If error still occurs - delete the database, create new and then run above commands. If got still same error - delete the database and all migration files and then run the commands.
PS You probably want DateField not DateTimeField for birthday storage :)
Related
I'm running into some migration problems.
I've tried deleting my last migration file, going into psql and dropping all the new tables and deleting the specific migration row in django_migrations.
But I'm still getting the following errors for the following model:
# my model
class Excerpt(models.Model):
id = models.UUIDField(
default=generate_ulid_as_uuid, primary_key=True, editable=False
)
body = models.JSONField(default=None)
slug = ArrayField(
models.SlugField(max_length=50), unique=True, null=True, blank=True
)
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column app_excerpt.slug does not exist
LINE 1: ..."app_excerpt"."chapter_id", "app_excerpt"."body", "app_excer...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py",
line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column app_excerpt.slug does not exist
LINE 1: ..."app_excerpt"."chapter_id", "app_excerpt"."body", "app_excer...
^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/code/manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py", line 55, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python3.9/site-packages/django/test/runner.py", line 725, in run_tests
old_config = self.setup_databases(aliases=databases)
File "/usr/local/lib/python3.9/site-packages/django/test/runner.py", line 643, in setup_databases
return _setup_databases(
File "/usr/local/lib/python3.9/site-packages/django/test/utils.py", line 179, in setup_databases
connection.creation.create_test_db(
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 90, in create_test_db
self.connection._test_serialized_contents = self.serialize_db_to_string()
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 136, in serialize_db_to_string
serializers.serialize("json", get_objects(), indent=None, stream=out)
File "/usr/local/lib/python3.9/site-packages/django/core/serializers/__init__.py", line 129, in serialize
s.serialize(queryset, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/serializers/base.py", line 90, in serialize
for count, obj in enumerate(queryset, start=1):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 133, in get_objects
yield from queryset.iterator()
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 353, in _iterator
yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1178, in execute_sql
cursor.close()
psycopg2.errors.InvalidCursorName: cursor "_django_curs_140256357783360_sync_19" does not exist
The strangest part is that I can see the slug column in my app_excerpt table (and I can even add objects to it via django's shell):
# python manage.py shell
from app.models import *
Excerpt.objects.create(body="hi")
Excerpt.objects.create(body="hello", slug=["hello"])
id | body | slug
--------------------------------------+---------+---------
018086f0-7f92-73c5-0cd0-3205fd36ae6f | "hi" |
018086f5-1324-9d3a-43ce-2dd66748f811 | "hello" | {hello}
So why is Django unable to see that column?
I was running python manage.py test --k which was using the old database. Running python manage.py test and recreating a new database fixes it.
I moved these to models to chat app
class Message(models.Model):
sender = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name="فرستنده")
receiver = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name="گیرنده", related_name="reciver")
text = models.CharField("متن", max_length=500)
date_and_time = models.DateTimeField(auto_now_add=True)
subject=models.CharField(max_length=200,verbose_name="موضوع")
notification=models.OneToOneField(Notification,on_delete=models.DO_NOTHING,related_name="message",null=True)
class Meta:
verbose_name = "پیام"
verbose_name_plural = "پیام ها"
class Response(Message):
darkhast_sabt_sherekat = models.ForeignKey(SabteSherkat,on_delete=models.CASCADE,related_name="responses")
After that, it raise an error saying the local id of Response crashed with message I made everything undo and this error keeps coming up
*
Operations to perform:
Apply all migrations: accounts, admin, auth, consultation, contenttypes, sessions, utilities, web
Running migrations:
Applying web.0007_auto_20210815_0831...Traceback (most recent call last):
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: near ")": syntax error
The above exception was the direct cause of the following exception:
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 "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/home/hamed/sqh-source/venv/lib/python3.8/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 "/home/hamed/sqh-source/venv/lib/python3.8/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 "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 178, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 346, in remove_field
self._remake_table(model, delete_field=field)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 283, in _remake_table
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/hamed/sqh-source/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
*
I deleted every pycache and my DB and even my virtualenv but this error keeps coming up!!!!
Add abstract = True in Message models Meta class, maybe that's the issue.
class Message(models.Model):
class Meta:
verbose_name = "پیام"
verbose_name_plural = "پیام ها"
abstract = True
I'm quite new to Django and ran into a problem while trying to create a custom user.
I followed all the steps outlined to create one, but because I initially started out with the default User model I deleted all my migrations and my postgres db to start from scratch (if not, I read it would cause problems).
makemigrations works fine, but then when I want to migrate I get the following error:
django.db.utils.ProgrammingError: relation "cyclabApp_customuser" does not exist.
Applying admin.0001_initial...Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "cyclabApp_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/usr/local/lib/python3.9/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 "/usr/local/lib/python3.9/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 "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 229, in apply_migration
migration_recorded = True
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 115, in __exit__
self.execute(sql)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
My docker container also outputs the following:
2021-04-12 10:22:13.665 UTC [95] STATEMENT: ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_user_id_c564eba6_fk_cyclabApp_customuser_id" FOREIGN KEY ("user_id") REFERENCES "cyclabApp_customuser" ("id") DEFERRABLE INITIALLY DEFERRED
I've looked everywhere for similar problems and their solutions, but no luck so far in trying to fix this issue...
Thanks in advance for any help!!
Ok, I solved it.
Apparently I was missing the model for my UserManager. Worked after I added that. :-)
class UserManager(BaseUserManager):
def create_user(
self, email, first_name, last_name, password=None,
commit=True):
"""
Creates and saves a User with the given email, first name, last name
and password.
"""
if not email:
raise ValueError(_('Users must have an email address'))
if not first_name:
raise ValueError(_('Users must have a first name'))
if not last_name:
raise ValueError(_('Users must have a last name'))
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
if commit:
user.save(using=self._db)
return user
I'm setting up a database in django, while trying to flush the database i get the following error:
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the "/home/luc/projects/PySorge/API/db.sqlite3" database,
and return each table to an empty state.
Are you sure you want to do this?
Type yes to continue, or no to cancel: yes
Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: django_content_type.name
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/flush.py", line 80, in handle
emit_post_migrate_signal(verbosity, interactive, database)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/sql.py", line 48, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/auth/management/__init__.py", line 42, in create_permissions
create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/contenttypes/management/__init__.py", line 132, in create_contenttypes
ContentType.objects.using(using).bulk_create(cts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 506, in bulk_create
returned_columns = self._batched_insert(
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1277, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: django_content_type.name
While trying to migrate, the following error occured:
Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
Applying api.0001_initial...Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: ID
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/luc/.local/lib/python3.9/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 "/home/luc/.local/lib/python3.9/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 "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: ID
I'm using django version 3.1.6. I'm not really sure how to fix this as I'm not very familiar with databases. Seems like the error is caused by a duplicate field id - my current database setup does have a field named ID.
My recommendation, although certainly not the best way forward, would be to delete the database (delete the actual sqlite3 file in your project) then delete all of your previous migrations, and then try making migrations again.
I had a very old Django site (still had South migrations in tree!) that had been ticking along on a SQLite database forever. At some point Django removed the name column from the django_content_types table. The model uses a property now based on the reflected model class. I've no idea why this hadn't be caught by a Django migration, but I've been known to fake migrations in the blood mist of getting something to run, no-matter-what.
I just dropped the column in ./manage.py dbshell:
ALTER TABLE django_content_type DROP COLUMN name;
And migrations worked again. No ill effects felt. All my old contenttypes are in there, everything still works.
python manage.py reset_db
This will delete all tables. It also helps to delete all migration files in the project if you want a complete wipeout
What is wrong?
C:\holamundo>python manage.py makemigrations App
Migrations for 'App':
App\migrations\0002_auto_20181216_0745.py
- Alter field nombre on tabla
resultado:
C:\holamundo>python manage.py migrate
Operations to perform:
Apply all migrations: App, admin, auth, contenttypes, sessions
Running migrations:
Applying App.0001_initial...Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\sqlite3\base.py", line 294, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: ID
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\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\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\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\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\migrations\operations\models.py", line 91, in database_forwards
schema_editor.create_model(model)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\base\schema.py", line 312, in create_model
self.execute(sql, params or None)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\base\schema.py", line 133, in execute
cursor.execute(sql, params)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\backends\sqlite3\base.py", line 294, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: ID
the table:
from django.db import models
# Create your models here.
class tabla (models.Model):
ID = models.PositiveSmallIntegerField()
nombre = models.CharField(max_length=50)
edad = models.PositiveSmallIntegerField()
comida = models.PositiveSmallIntegerField()
cantidad = models.PositiveSmallIntegerField()
def datos(self):
cadena="{0}, {1} {2} {3} {4}"
return cadena.format(self.ID, self.nombre,self.edad,self.comida,self.cantidad)
def __str__(self):
return self.datos()
You need to add primary_key=True if you want to override the id field.
id = models.PositiveIntegerField(primary_key=True)