Django更改数据库中的迁移表名称

Django更改数据库中的迁移表名称,django,migration,Django,Migration,如何设置django_迁移表名 甚至我也编辑了 venv.lib.site-packages.django.db.migrations.recorder>MigrationRecorder>meta=db\u table=“ABC\u django\u migrations” makemigrations无法检测到更改 我使用的是Django版本:3.0.5 只需确保数据库中存在下表 def migrate(self, targets, plan=None, state=None, fake=F

如何设置django_迁移表名

甚至我也编辑了

venv.lib.site-packages.django.db.migrations.recorder>MigrationRecorder>meta=db\u table=“ABC\u django\u migrations”

makemigrations无法检测到更改


我使用的是Django版本:3.0.5

只需确保数据库中存在下表

def migrate(self, targets, plan=None, state=None, fake=False, fake_initial=False):

    self.recorder.ensure_schema()

    ....
在哪里创建表

def ensure_schema(self):
    """Ensure the table exists and has the correct schema."""
    # If the table's there, that's fine - we've never changed its schema
    # in the codebase.
    if self.has_table():
        return
    # Make the table
    try:
        with self.connection.schema_editor() as editor:
            editor.create_model(self.Migration)
    except DatabaseError as exc:
        raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

您可以手动进行迁移以编辑此模型(
AlterModelTable
或自定义sql),但我不建议更改有关迁移的任何内容

如何解决此问题:

  • 安装应用程序

    pip install django-db-prefix
    
  • 将应用程序包括在Settings.py中

    INSTALLED_APPS = ['django_db_prefix',]
    
    DB_PREFIX = "foo_"
    
  • 在Settings.py中添加前缀

    INSTALLED_APPS = ['django_db_prefix',]
    
    DB_PREFIX = "foo_"
    

  • 为什么要自定义
    django_migrations
    表的名称?@DavitTovmasyan由于我客户的政策,我只能在一个数据库中创建多个项目。如果我不为项目A重命名django_migrations表名,我担心将来在一台服务器或一个数据库上生成另一个项目时会发生冲突。@WKenny(这似乎是不可能的)?如果是postgreSQL,也可以使用模式。也有人尝试在所有django表前面加前缀,比如@DavitTovmasyan,我知道这听起来很疯狂。不管怎样,前缀,我真的不明白它是干什么用的?我是否可以重命名此数组中的所有默认DB表名,如Admin、Auth、Content\u type、Session、Token?@WKenny它会在所有表名上附加前缀,如myprefix\u django\u Session、myprefix\u myapp\u mymodel等。。