romainm
romainm

Reputation: 281

"relation "social_auth_code" does not exist" when applying migrations on django

I recently switched from django-social-auth to python-social-auth, but it has clearly damage my migrations system. any time I try to migrate changes I got this one :

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 457, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 603, in _alter_field
    params,
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 103, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "social_auth_code" does not exist

The problem being that it only happens in the production version of my app and that for some other reasons, I had to delete my migrations file in the past. Doesn't make it easy to investigate. Anyway, it perfectly work now with my development app but I can't figure out what can be the problem in production, I tried all the "faking migrations" tricks in the world and nothing seems to work.

the only place in the web were I can find such an error is there but I never used South, so the first answer is not working for me. Directly digging into the migrations table and sending raw SQL instructions could be the solution but since it is in my production version, I don't feel confortable with tinkering the db ( I got thousands of registered users, there data and all..). In short, I'm in deep sh*t :). Also, I don't know which command to use to directly access the migration table in the db...

Any solution that keeps my data safe is more than welcome :))

Upvotes: 2

Views: 911

Answers (2)

Jj.
Jj.

Reputation: 3160

I tried to run the solution before and had a bit of difficulties with the dependency. I ended up just prepending1 the following lines to the Migration.operations list in migration 0007_code_timestamp (exactly as the original first answer suggested)

        migrations.CreateModel(                                                 
            name='Code',                                                        
            fields=[                                                            
                ('id', models.AutoField(                                        
                    verbose_name='ID', serialize=False, auto_created=True,      
                    primary_key=True)),                                         
                ('email', models.EmailField(max_length=75)),                    
                ('code', models.CharField(max_length=32, db_index=True)),       
                ('verified', models.BooleanField(default=False)),               
            ],                                                                  
            options={                                                           
                'db_table': 'social_auth_code',                                 
            },                                                                  
            bases=(models.Model, social_django.storage.DjangoCodeMixin),        
        ),                                                                      
        migrations.AlterUniqueTogether(                                         
            name='code',                                                        
            unique_together=set([('email', 'code')]),                           
        ),       

And make sure to import social_django at the top.

That solved it for me, it was easier than creating a new migration and dealing with the dependency clarification.

Upvotes: 0

lexaf
lexaf

Reputation: 36

When migrating to python_social_auth I got the same error. This is useful for Django 1.8. Maybe my solution will help you:

  1. Fake migrate initial of python_social_auth

    python manage.py migrate default 0001 --fake
    
  2. Create yourself migration for initial psa and put it to /your_project/your_app/migrations/0009_migrate_to_psa.py:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    from django.db import models, migrations
    import social.apps.django_app.default.fields
    from django.conf import settings
    import social.storage.django_orm
    from social.utils import setting_name
    
    user_model = getattr(settings, setting_name('USER_MODEL'), None) or \
                 getattr(settings, 'AUTH_USER_MODEL', None) or \
                 'auth.User'
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            migrations.swappable_dependency(user_model),
            ('your_app', '0008_last_migration_in_your_app'),
            ('default', '0001_initial'),
        ]
    
        operations = [
            migrations.CreateModel(
                name='Code',
                fields=[
                    ('id', models.AutoField(
                        verbose_name='ID', serialize=False, auto_created=True,
                        primary_key=True)),
                    ('email', models.EmailField(max_length=75)),
                    ('code', models.CharField(max_length=32, db_index=True)),
                    ('verified', models.BooleanField(default=False)),
                ],
                options={
                    'db_table': 'social_auth_code',
                },
                bases=(models.Model, social.storage.django_orm.DjangoCodeMixin),
            ),
            migrations.AlterUniqueTogether(
                name='code',
                unique_together=set([('email', 'code')]),
            ),
        ]
    

    Notice the dependencies:

    dependencies = [
        migrations.swappable_dependency(user_model),
        ('your_app', '0008_last_migration_in_your_app'),
        ('default', '0001_initial'),
    ]
    
  3. Migrate your project

    python manage.py migrate your_app
    
  4. And migrate all

    python manage.py migrate
    

UPDATE: Unfortunately, this method requires a model Code in the file models.py your application. Otherwise the table will be deleted from the database when the next operation makemigrations. /your_project/your_app/models.py:

from social.storage.django_orm import DjangoCodeMixin
class Code(models.Model, DjangoCodeMixin):
    email = models.EmailField(max_length=254)
    code = models.CharField(max_length=32, db_index=True)
    verified = models.BooleanField(default=False)

    class Meta:
        db_table = 'social_auth_code'
        unique_together = ('email', 'code')

Upvotes: 2

Related Questions