Switching from development database(SQLite) from production(PostgreSQL)

Hi everyone,
I built a website that I’m trying to push into production now. So I’m trying to host it in a VPS ( Ubuntu 22.04.5 LTS)and given I have to change the database for production, I installed PostgreeSQL, I created a new database on my linux server using psql, I also modified my database settings to make it match with my early created database. Then I simply tried to use the basic manage.py migrate command to see if it works and then I get the error:

 File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/models/query.py", line 649, in get
    raise self.model.DoesNotExist(
__fake__.Permission.DoesNotExist: Permission matching query does not exist.

Here is the complete traceback:

Operations to perform:
  Apply all migrations: account, admin, auth, authentication, contenttypes, django_celery_results, scholar, sessions, socialaccount
Running migrations:
  Applying authentication.0004_auto_20240303_1331...Traceback (most recent call last):
  File "/home/ubuntu/sh/Scholar-HUB/main/blur/manage.py", line 23, in <module>
    main()
  File "/home/ubuntu/sh/Scholar-HUB/main/blur/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/ubuntu/sh/Scholar-HUB/main/blur/authentication/migrations/0004_auto_20240303_1331.py", line 11, in create_groups
    add_post = Permission.objects.get(codename='add_post')
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/django/db/models/query.py", line 649, in get
    raise self.model.DoesNotExist(
__fake__.Permission.DoesNotExist: Permission matching query does not exist.

Can someone help me to figure out what’s wrong with my settings ?
Here we have some of my settings

pg_hba.conf file:

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

django datablase config file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', 
        'NAME': config('POSTGRESQL_DATABASE_NAME'),
        'USER': config('POSTGRESQL_USERNAME'),
        'PASSWORD': config('POSTGRESQL_PASSWORD'),
        'HOST': config('POSTGRESQL_HOST'),
        'PORT': config('POSTGRESQL_PORT'),    
    }
}

And here is the problematic migration file:

from django.db import migrations


def create_groups(apps, schema_migration):
    User = apps.get_model('authentication', 'User')
    Group = apps.get_model('auth', 'Group')
    Permission = apps.get_model('auth', 'Permission')

    add_post = Permission.objects.get(codename='add_post')
    view_post = Permission.objects.get(codename='view_post')
    change_post = Permission.objects.get(codename='change_post')
    delete_post = Permission.objects.get(codename='delete_post')

    publisher_permissions = [
        add_post,
        view_post,
        change_post,
        delete_post,
    ]

    moderator_permissions = [
        view_post,
        delete_post,
    ]

    publishers = Group(name='publishers')
    publishers.save()
    publishers.permissions.set(publisher_permissions)

    moderators = Group(name='moderators')
    moderators.save()
    moderators.permissions.set(moderator_permissions)

    elders = Group(name='elders')
    elders.save()
    elders.permissions.add(view_post)

    viewers = Group(name='viewers')
    viewers.save()
    viewers.permissions.add(view_post)

    for user in User.objects.all():
        pass
        # if user.role == 'PUBLISHER':
        #     publishers.user_set.add(user)
        # elif user.role == 'MODERATOR':
        #     moderators.user_set.add(user)
        # elif user.role == 'ELDER':
        #     elders.user_set.add(user)
        # elif user.role == 'VIEWER':
        #     viewers.user_set.add(user)


class Migration(migrations.Migration):

    dependencies = [
        ('authentication', '0003_alter_user_role'),
    ]

    operations = [
        migrations.RunPython(create_groups)
    ]

Hi,

Your create_groups() operation is using models from the auth app but that app is not declared as a dependency of your migration.

I think what ends up happening is that Django is trying to run this migration before any migration from auth has been run, and so the table for the Permission model is empty.

Try adding a new line ('auth', '0012_alter_user_first_name_max_length'), to your migration’s dependencies and see if that fixes it.

Hi, thanks for the reply. I try it and I make you a feedback

Hi, sorry for the delay. I applied your advice but the result remains unchanged

This is the query throwing the error as shown in the traceback:

That’s from this section of the migration file:

Where are these permissions being created? (Which migration file?) Has that one been applied? Can you locate these entries directly in the database?

Also note that codenames aren’t necessarily unique. You run the risk of getting multiple results with a get if more than one model has created a permission with one of those names.
You want to query the Permission model with both the codename and the content_type.