model.py
class Staff(models.Model):
organization = models.ForeignKey(
Organization, related_name='staff_organization', on_delete=models.CASCADE, blank=True)
user = models.OneToOneField(
User, on_delete=models.CASCADE, blank=True)
phone = models.CharField(max_length=15, blank=True)
address = models.CharField(max_length=200, blank=True)
I am trying to add the new field in the database table. II just added the address field. I got following 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 "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "D:\Project\AIT Project\project\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 "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Project\AIT Project\project\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user_email_1c89df09_uniq" already exists
try to run
python manage.py migrate <yourappname> zero
then go into you migration folder and delete all the file except pycache and initpy
then again
python manage.py makemigrations
python manage.py migrate
this has to solve your problem
One option is to delete auth_user_email_1c89df09_uniq constraint from db in any database management system. i did it with pgadmin 2.
Related
I have a django abstract model that goes like this:
class AbstractAppendOnly(models.Model):
created_at = models.DateTimeField(auto_now=True, editable=False)
db_note = models.TextField(default=None, null=True)
class Meta:
abstract = True
This model is extended to multiple other models so that a create_at field is automatically added to all of those. Now when an object is created and saved from django server end the created_at timestamp field is automatically produced, as expected. But it does not enforce it in database level, so anyone can insert a row with fake created_at value.
As far as I am concerned django does not let user to set database level default value for model issue-470.
What I have found that may solve the issue -
I have found an way to customize the migration files using tool like django-add-default-value . But since migrations may sometimes needed to be pruned in big systems and we will have to write customized migrations every time we create a new model, it seems of huge error-prone.
Another way I have thought of is to add trigger using django-pgtrigger like this
#pgtrigger.register(
pgtrigger.Trigger(
name='insert_auto_created_at_timestamp',
operation=pgtrigger.Insert,
when=pgtrigger.Before,
func='''
new.created_at = now();
return new;
'''
)
)
class AbstractAppendOnly(models.Model):
created_at = models.DateTimeField(auto_now=True, editable=False)
db_note = models.TextField(default=None, null=True)
class Meta:
abstract = True
But doing so throws error while running migrations
Traceback (most recent call last):
File "/run/media/shafi/Codes/Python/tkdc/./manage.py", line 22, in <module>
main()
File "/run/media/shafi/Codes/Python/tkdc/./manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 267, in handle
emit_post_migrate_signal(
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/core/management/sql.py", line 48, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/pgtrigger/apps.py", line 9, in install
pgtrigger.install()
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/pgtrigger/core.py", line 846, in install
trigger.install(model)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/pgtrigger/core.py", line 621, in install
cursor.execute(rendered_comment)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/run/media/shafi/Codes/Python/tkdc/venv/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 "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/run/media/shafi/Codes/Python/tkdc/venv/lib/python3.9/site-packages/pgconnection/core.py", line 85, in execute
return super().execute(sql, args)
django.db.utils.ProgrammingError: relation "utils_abstractappendonly" does not exist
Which I think is because AbstractAppendOnly is an abstract class, and the trigger translates directly to the Model it is attached to and not to the inherited class. Therefore one solution would to add the trigger to every model that extends the class.
I would be grateful if someone could point me out a solution.
I'm trying to store an ArrayField in a Django model called User, I'm using MongoDB and Djongo.
I managed to do it with one model called Pair, I can store a list of Pairs and store that list in a User model using ArrayField which was fairly simple, I just followed this example from Djongo's official doc
Now I'm trying to store another list but for some reason, it just keeps giving me a huge TraceBack (90 lines) with an ambiguous error: 'django.db.utils.DatabaseError'
here's my user model:
class User(AbstractBaseUser):
email = models.EmailField(verbose_name='email address', max_length=127, unique=True)
first_name = models.CharField(verbose_name='First Name', max_length=30, blank=True)
last_name = models.CharField(verbose_name='Last Name', max_length=30, blank=True)
is_active = models.BooleanField(default=True, verbose_name='Active')
is_admin = models.BooleanField(default=False, verbose_name='Admin')
pairs = models.ArrayField (
model_container=Pair,
null=True
)
conversations = models.ArrayField(
model_container=Conversation,
null=True
)
again, the pairs attribute is working just fine but the conversation is not
The Pair model
class Pair(models.Model):
name = models.CharField(max_length=16)
purchase_price = models.DecimalField(verbose_name='Purchase price', max_digits=20, decimal_places=6)
class Meta:
ordering = ['name']
abstract=True
The Conversation model
class Conversation(models.Model):
handle = models.CharField(max_length=255)
class Meta:
abstract = True
and this is the traceback:
Operations to perform:
Apply all migrations: accounts, admin, auth, contenttypes, sessions, tracker
Running migrations:
Not implemented alter command for SQL ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
Applying accounts.0003_user_conversations...Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute
self.result = Query(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 783, in __init__
self._query = self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 875, in parse
raise e
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 856, in parse
return handler(self, statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 888, in _alter
query = AlterQuery(self.db, self.connection_properties, sm, self._params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 425, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 84, in __init__
super().__init__(*args)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 62, in __init__
self.parse()
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 435, in parse
self._add(statement)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 598, in _add
raise SQLDecodeError(err_key=tok.value,
djongo.exceptions.SQLDecodeError:
Keyword: array
Sub SQL: ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL
FAILED SQL: ('ALTER TABLE "accounts_user" ADD COLUMN "conversations" array NULL',)
Params: ([],)
Version: 1.3.4
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
djongo.database.DatabaseError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "/home/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 110, in database_forwards
schema_editor.add_field(
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 480, in add_field
self.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.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/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/akram/Desktop/cointracker/.venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
Note: when I run python manage.py migrate, migrations are created with no problem, but when I apply them, I get the above error
Problem solved!
turned out that there's a problem with Djonogo specifically in this file djongo/sql2mongo/query.py
this question has already been answered in this comment on the djongo Github page
I added new fields and file fields and when I ran migrations I got this error.Looks like I am getting error in a FileField .I don't know what's causing this issue.Please help!
Applying users.0010_auto_20200228_1138...Traceback (most recent call last):
File "C:\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\Python38\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__users_personal_detail.husband_adhaarcopy
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 "C:\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "C:\Python\Python38\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "C:\Python\Python38\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:\Python\Python38\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:\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python\Python38\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:\Python\Python38\lib\site-packages\django\db\migrations\operations\fields.py", line 110, in database_forwards
schema_editor.add_field(
File "C:\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 283, in _remake_table
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
File "C:\Python\Python38\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "C:\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Python\Python38\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:\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
File "C:\Python\Python38\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\Python38\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__users_personal_detail.husband_adhaarcopy
this is how my models.py looks like.I have given blank=True and null=True for the file field as well,and i think i should have worked fine but when i run python manage.py migrate I get this error.
class Personal_Detail(models.Model):
beneficiary_adhaar_name=models.TextField(blank=True, null=True)
adhaarno=models.IntegerField(blank=True, null=True)
adhaarcopy = models.FileField(upload_to='adhaar/', null=True, blank=True,)
idcard=models.TextField(blank=True, null=True)
adhaar_eid=models.IntegerField(blank=True,null=True)
beneficiary_id_name=models.TextField(blank=True, null=True)
idno=models.IntegerField(blank=True, null=True)
idcopy=models.FileField(upload_to='identitycard/', null=True, blank=True,)
husband_adhaar_name=models.TextField(blank=True, null=True)
husband_adhaarno=models.IntegerField(blank=True, null=True)
husband_adhaarcopy = models.FileField(upload_to='adhaar/', null=True, blank=True,)
husband_idcard=models.TextField(blank=True, null=True)
husband_adhaar_eid=models.IntegerField(blank=True,null=True)
husband_id_name=models.TextField(blank=True, null=True)
husband_idno=models.IntegerField(blank=True, null=True)
husband_idcopy=models.FileField(upload_to='identitycard/', null=True, blank=True,)
def __str__(self):
return self.beneficiary_adhaar_name or self.beneficiary_id_name or str(self.pk)
First delete all the migrations in your migrations folder except init.py file and then run makemigrations and migrate commands.
hope it works
Migrate error after add new field use django-pgcrypto-fields
My model (migrate okay in the first time)
class Dkm(models.Model):
name = fields.TextPGPSymmetricKeyField()
value = fields.IntegerPGPSymmetricKeyField(default=0)
I update model and migrate again:
class Dkm(models.Model):
name = fields.TextPGPSymmetricKeyField()
value = fields.IntegerPGPSymmetricKeyField(default=0)
value3 = fields.IntegerPGPSymmetricKeyField(default=0)
Error occur
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\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\core\management\base.py", line 364, in execute
cursor.execute(sql, params)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\vu.tran\Desktop\kona-server\env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "value3" is of type bytea but default expression is of type integer
HINT: You will need to rewrite or cast the expression.
The error will not occur if set null=True
The error
column "value3" is of type bytea but default expression is of type integer
refers to the default=0 thing in your fields definition.
And the reason that it appears only while the second time you migrate with added field value3 is because it would be actually looking for the default values for already existing records. Whereas there doesn't exist any record for the first time you migrate.
In my multi-tenant site, I have the following database structure:
Public (shared):
tenants
domains
users
Test:
users
clients
projects
projectusers
tasks
Test2:
users
clients
projects
projectusers
tasks
This is a simplified list - all three schemas include (among others) auth_permissions, auth_group, and auth_group_permissions.
The projectusers table (cross-ref that ties users to projects), has a One-to-One field for the User table, and a ForeignKey to the projects table, as follows:
class ProjectUser(DateFieldsModelMixin, models.Model):
user = models.OneToOneField(
User,
on_delete=models.PROTECT,
verbose_name=_('User'),
related_name='user_projects',
)
project = models.ForeignKey(
Project,
on_delete=models.PROTECT,
verbose_name=_('Project'),
related_name='project_users',
)
...other fields omitted...
The point is, the reference from projectusers to users is defined in the ProjectUser model. There's no reference from User model back to ProjectUser.
I wrote a load_fixtures (management) command to, well, load fixtures to set up our dev site(s) with the data we'd need to do preliminary testing (before writing/running unit-tests).
One of the things that command does is empty the auth_permissions table, reset the auto-increment value back to 1, and then import its fixture so that all IDs match across local dev sites, and (eventually) in production. This is necessary because fixtures for auth_group and auth_group_permissions reference permissions by ID.
The error I'm getting occurs during the delete process on that table when running in the "public" schema. Note there is no projectusers table in Public. This is the line that does the delete on auth_permissions:
model.objects.all().delete()
That is line 215 in the load_fixtures command, in the clear_table() method. You can see that line referenced in the traceback:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/code/src/virticl_api/utilities/management/commands/load_fixtures.py", line 148, in handle
self.clear_table(file_path, self.clear_tables[file_name])
File "/code/src/virticl_api/utilities/management/commands/load_fixtures.py", line 215, in clear_table
model.objects.all().delete()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 662, in delete
collector.collect(del_query)
File "/usr/local/lib/python3.6/site-packages/django/db/models/deletion.py", line 220, in collect
elif sub_objs:
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __bool__
self._fetch_all()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "projects_projectuser" does not exist
LINE 1: ...ourly_rate", "projects_projectuser"."status" FROM "projects_...
So why is that delete method, running in the Public schema, finding a relationship to the projectuser table?