Django迁移列与引用列不同

发布于 2025-02-11 11:13:53 字数 2745 浏览 2 评论 0原文

我有以下模型,该模型是现有的DB模型,并通过Django的InspectDB管理命令创建了以下模型。

class ExistingLegacyModel(models.Model):
    period = models.TextField(db_column="Period", blank=True, null=True) 
    key = models.AutoField(db_column="OutlookKey", primary_key=True)
    class Meta:
        managed = False
        db_table = "table_name"

目前,我正在尝试使用现有旧版数据库模型的字段外键引用一个模型,

class TestModel(models.Model):
    period = models.ForeignKey(
        ExistingLegacyModel,
        on_delete=models.CASCADE,
        db_column="OutlookKey",
    )

因此当我运行makemigrations命令时,迁移文件将成功创建而没有问题。以下是迁移文件内容。


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('historical', '0011_phoenixcontractprice'),
    ]

    operations = [
        migrations.CreateModel(
            name='TestModel',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('period', models.ForeignKey(db_column='OutlookKey', on_delete=django.db.models.deletion.CASCADE, to='app.ExistingLegacyModel')),
            ],
        ),
    ]

因此,现在当我现在运行迁移命令时,它正在失败并给出以下错误。

django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'table_name.OutlookKey' is not the same data type as referencing column 'version_testmodel.OutlookKey' in foreign key 'version_testmodel_OutlookKey_eb16c31c_fk_table_name_OutlookKey'. (1778) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")

在过去的几天里,我遇到了这个问题,我在互联网上进行了搜索,但没有任何解决方案。我发现了几个与我的问题非常相似的stackoverflow问题,但是这些问题也没有解决。

  1. django- /a>
  2. django 3.2与外国密钥关系的兼容性

目前,我正在使用Django 3.2.13和MSSQL-DJANGO连接到MSSQL数据库。

对此的任何帮助将不胜感激!先感谢您。

更新1

我运行了SQLMigrate命令以进行初始迁移。因此,对于周期列,它使用Big Int [OutlookKey] Bigint而不是Null创建表格,而现有旧模型则具有正常的整数字段。

ALTER TABLE [<app>_<model>] ADD CONSTRAINT [<app>_<model>_OutlookKey_3505d410_fk_<existing_legacy_table>_OutlookKey] FOREIGN KEY ([OutlookKey]) REFERENCES [<existing_legacy_table>] ([OutlookKey]);

I have the below model which is an existing DB model and through Django's inspectdb management command the below model is created.

class ExistingLegacyModel(models.Model):
    period = models.TextField(db_column="Period", blank=True, null=True) 
    key = models.AutoField(db_column="OutlookKey", primary_key=True)
    class Meta:
        managed = False
        db_table = "table_name"

and currently, I'm trying to create a model with a field foreign key reference to the existing legacy DB model

class TestModel(models.Model):
    period = models.ForeignKey(
        ExistingLegacyModel,
        on_delete=models.CASCADE,
        db_column="OutlookKey",
    )

so when I run the makemigrations command the migration file is successfully getting created with no issue. below is the migration file content.


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('historical', '0011_phoenixcontractprice'),
    ]

    operations = [
        migrations.CreateModel(
            name='TestModel',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('period', models.ForeignKey(db_column='OutlookKey', on_delete=django.db.models.deletion.CASCADE, to='app.ExistingLegacyModel')),
            ],
        ),
    ]

so now when i run the migrate command now, it is failing and giving the below error.

django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'table_name.OutlookKey' is not the same data type as referencing column 'version_testmodel.OutlookKey' in foreign key 'version_testmodel_OutlookKey_eb16c31c_fk_table_name_OutlookKey'. (1778) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")

I'm stuck with this issue for the past couple of days and I searched all over the internet but didn't get any resolution. I found a couple of StackOverflow questions that are very similar to my issue, but those questions are also unanswered.

  1. Django - Migration foreign key field type not matching current type
  2. Django 3.2 update AutoField to BigAutoField backward compatibility with foreign key relations

I'm currently using Django 3.2.13 and mssql-django to connect to the MSSQL database.

Any help on this will be highly appreciated! Thank you in advance.

UPDATE 1

I ran the sqlmigrate command for the initial migration. So for the period column, it is creating the table with a foreign key field with big int [OutlookKey] bigint NOT NULL whereas the existing legacy model has a normal integer field.

ALTER TABLE [<app>_<model>] ADD CONSTRAINT [<app>_<model>_OutlookKey_3505d410_fk_<existing_legacy_table>_OutlookKey] FOREIGN KEY ([OutlookKey]) REFERENCES [<existing_legacy_table>] ([OutlookKey]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文