首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django-租户“无法创建django_migrations表”

Django-租户“无法创建django_migrations表”
EN

Stack Overflow用户
提问于 2022-10-02 22:45:40
回答 1查看 49关注 0票数 0

当我们的生产数据库由于迁移过程中的错误而没有被迁移时,就出现了一个问题。此错误涉及包django-tenants的使用,该包是django-tenant-schemas包的分叉。

错误:

代码语言:javascript
复制
Traceback (most recent call last):
File "/backend/manage.py", line 21, in <module>
    main()
  File "/backend/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.9/site-packages/django_tenants/management/commands/migrate_schemas.py", line 89, in handle
    executor.run_migrations(tenants=tenants)
  File "/usr/local/lib/python3.9/site-packages/django_tenants/migration_executors/standard.py", line 14, in run_migrations
Starting new HTTPS connection (1): o1380729.ingest.sentry.io:443
    run_migrations(self.args, self.options, self.codename, schema_name, idx=idx, count=len(tenants))
  File "/usr/local/lib/python3.9/site-packages/django_tenants/migration_executors/base.py", line 45, in run_migrations
    migration_recorder.ensure_schema()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 70, in ensure_schema
    raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (relation "django_migrations" already exists
)

是什么导致了这个错误?

EN

回答 1

Stack Overflow用户

发布于 2022-10-02 22:45:40

在错误发生之前,有几个租户确实迁移了,这一事实告诉您,错误是由特定的租户引起的。要找出错误的来源,我们需要跟踪跟踪。在第二行到最后一行,我们看到:

代码语言:javascript
复制
File "/usr/local/lib/python3.9/site-packages/django_tenants/migration_executors/standard.py", line 14, in run_migrations 
Starting new HTTPS connection (1): o1380729.ingest.sentry.io:443 run_migrations(self.args, self.options, self.codename, schema_name, idx=idx, count=len(tenants)) 
File "/usr/local/lib/python3.9/site-packages/django_tenants/migration_executors/base.py", line 45, in run_migrations migration_recorder.ensure_schema() 
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py"

因此,看看django_tenants源代码,在/usr/local/lib/python3.9/site-packages/django_tenants/migration_executors/base.py第45行附近,我们可以看到以下代码:

代码语言:javascript
复制
    connection = connections[options.get('database', get_tenant_database_alias())]
    connection.set_schema(schema_name, tenant_type=tenant_type)

    # ensure that django_migrations table is created in the schema before migrations run, otherwise the migration
    # table in the public schema gets picked and no migrations are applied
    migration_recorder = MigrationRecorder(connection)
    migration_recorder.ensure_schema() # line 45

由于schema_name被传递给connection.set_schema(schema_name, tenant_type=tenant_type),所以我们可以上一行并放置一个print语句来打印正在迁移的模式的名称。此print语句将在迁移期间显示在控制台上,并显示有问题的架构。在我们的示例中,由于模式名称中大写的不同,允许使用重复的架构名称。

为了使用python manage.py shell更改模式名称,我们使用instance = TenantModel.objects.get(schema_name="conflicting_name")导入租户模型,并使用instance.schema_name("new_name")将模式设置为不同的名称,并在实例上调用instance.save()

这运行了一个迁移并解决了这个问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73929848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档