0

I just attempted to migrate my django (1.9 with python3.4) app from Sqlite3 to Postgresql. Upon migrating ./manage.py makemigrations "app" and ./manage.py migrate and then use loaddata to load back all the users, everything seemed to work find. However when I attempt to authenticate a user with SessionAuthentication I receive the following error:

django.db.utils.IntegrityError: insert or update on table "server_gcssession" violates foreign key constraint "server_gcsses_session_id_6afc9a27_fk_django_session_session_key" DETAIL: Key (session_id)=(b2j6ip8ac29xcvbac7ul3q1wha6mnri0) is not present in table "django_session".

After dissecting the exception, it seems to be that when a user with SessionAuthentication attempts to authenticate via authenticate(username,password) and login(request,usermodel), the Session object is never actually stored in the table django_sessions. The Integrity exception is thrown when I attempt to use the user's session_id as a key into the following model:

class GCSSession(models.Model):
   user = models.ForeignKey(settings.AUTH_USER_MODEL)
   session =models.ForeignKey(Session)

with this query:

GCSSession.objects.get_or_create(
    user=user,
    session_id = request.session.session_key
)

which is triggered with a Django Signal upon user login

I've looked online and the closest thing I found to this problem was this question . However, I'm still not sure what to do, any ideas? Is there a step I missed for migrating?

Update:

This the output of running the above migration commands with sqlite3:

    Migrations for 'server':
  0001_initial.py:
    - Create model ImagingUser
    - Create model GCSSession
    - Create model Picture
    - Create model Target
Operations to perform:
  Apply all migrations: server, auth, contenttypes, admin, sessions
Running migrations:
  Rendering model states... DONE
  Applying sessions.0001_initial... OK
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying server.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK

This is the output of running the same commands with postgresql:

   Migrations for 'server':
  0001_initial.py:
    - Create model ImagingUser
    - Create model GCSSession
    - Create model Picture
    - Create model Target
Operations to perform:
  Apply all migrations: sessions, auth, admin, contenttypes, server
Running migrations:
  No migrations to apply.
2
  • pgloader will helps you to copy entire data from sqlite to postgresql. or please post your dumpdata loaddata commands Commented Jun 23, 2016 at 1:31
  • The only data that I'm restoring is just 5 users, with ./manage.py loaddata [app-name]/fixtures/users.json This fixture worked fine with sqlite3 Commented Jun 23, 2016 at 1:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.