gpt4 book ai didi

python - "relation "social_auth_code "does not exist"在 django 上应用迁移时

转载 作者:行者123 更新时间:2023-11-29 12:11:09 24 4
gpt4 key购买 nike

我最近从 django-social-auth 切换到 python-social-auth,但它显然损坏了我的迁移系统。每当我尝试迁移更改时,我都会得到这个:

  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

问题是它只发生在我的应用程序的生产版本中,并且由于某些其他原因,我过去不得不删除我的迁移文件。不方便调查。无论如何,它现在可以完美地与我的开发应用程序一起使用,但我无法弄清楚生产中的问题是什么,我尝试了世界上所有的“伪造迁移”技巧,但似乎没有任何效果。

我在网上唯一能找到这样的错误的地方是 there但我从未使用过南方,所以第一个答案对我不起作用。直接挖掘迁移表并发送原始 SQL 指令可能是解决方案,但由于它在我的生产版本中,我对修改数据库感到不舒服(我有成千上万的注册用户,那里有数据和所有......)。简而言之,我陷入了深渊 :)。另外,不知道用哪个命令直接访问数据库中的迁移表...

非常欢迎任何能确保我的数据安全的解决方案:))

最佳答案

当迁移到 python_social_auth 时,我遇到了同样的错误。这对 Django 1.8 很有用。也许我的解决方案会对您有所帮助:

  1. 伪造 python_social_auth 的迁移首字母

    python manage.py migrate default 0001 --fake
  2. 为初始 psa 创建自己的迁移并将其放入/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')]),
    ),
    ]

    注意依赖关系:

    dependencies = [
    migrations.swappable_dependency(user_model),
    ('your_app', '0008_last_migration_in_your_app'),
    ('default', '0001_initial'),
    ]
  3. 迁移您的项目

    python manage.py migrate your_app
  4. 并全部迁移

    python manage.py migrate

更新:不幸的是,此方法需要您的应用程序文件 models.py 中的模型代码。否则在下一次操作 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')

关于python - "relation "social_auth_code "does not exist"在 django 上应用迁移时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34221374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com