Django迁移不包含带有MSSQL数据库的复合外键关系和复合主键

时间:2019-03-20 19:54:21

标签: sql-server django-migrations

我只是使用现有的 MSSQL (不是mysql)数据库启动新的Django项目。在数据库中,我有四个表,它们都相互关联。我通过 python manage.py inspectdb(表_name)为现有表构建模型。但它既没有外键也没有OneOne的关系。因此,我根据表关系更新了models.py。

请注意,每个表都有复合主键和复合外键。我使用Django第三方库定义了带有unique_togather =()的复合主键和复合外键 Django-composite-foreignkey模块

但是当我尝试迁移时,它似乎没有建立联系

models.py

from django.db import models
from compositefk.fields import CompositeForeignKey, CompositeOneToOneField

class Company(models.Model):
    code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)   
    srccode  = models.SmallIntegerField(db_column='SrcCode')   
    est = models.DateTimeField(db_column='Est')   
    rownum = models.BigIntegerField(db_column='RowNum')  

class Meta:
    manage = False
    unique_together = ('code', 'srccode')
    db_table = 'Company'  

class Department(models.Model):
    depcode = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)    
    depsrccode  = models.SmallIntegerField(db_column='Depsrccode')   
    name  = models.CharField(db_column='Name')   
    rownum = models.BigIntegerField(db_column='RowNum')   
class Meta:
    manage = False
    unique_together = ('depcode', 'depsrccode')
    db_table = 'department'

class Floor(models.Model):
    code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)   
    srccode  = models.SmallIntegerField(db_column='SrcCode') 
    depcode = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)    
    depsrccode  = models.SmallIntegerField(db_column='Depsrccode')   
    floorname  = models.CharField(db_column='FloorName')   
    rownum = models.BigIntegerField(db_column='RowNum')
    company = CompositeForeignKey(Company,on_delete=models.CASCADE,to_fields={'code':'code','srccode': 'srccode'})
    department= CompositeOneToOneField(Department,on_delete=models.CASCADE,to_fields={'depcode':'depcode','depsrccode': 'depsrccode'})   

    class Meta:
        manage = False
        unique_together = ('depcode', 'depsrccode','floorname')
        db_table = 'floor'

class SubCompany(models.Model):
    code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)   
    srccode  = models.SmallIntegerField(db_column='SrcCode')
    subname  = models.CharField(db_column='SubName')   
    rownum = models.BigIntegerField(db_column='RowNum')
    company = CompositeForeignKey(Company,on_delete=models.CASCADE,to_fields={'code':'code','srccode': 'srccode'})

    class Meta:
        manage = False
        unique_together = ('code', 'srccode','subname','rownum')
        db_table = 'SubCompany'

在从inspectdb自动创建模型后,我添加了所有复合外键和主键约束。 我的初始迁移文件如下

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='SubCompany',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),  
                ('srccode'  = models.SmallIntegerField(db_column='SrcCode')),
                ('subname'  = models.CharField(db_column='SubName')),   
                ('rownum' = models.BigIntegerField(db_column='RowNum')),
                ],

            options={
                'db_table': 'SubCompany',
                'managed': False,
            },
        ),
        migrations.CreateModel(
            name='Company',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),  
                ('srccode'  = models.SmallIntegerField(db_column='SrcCode')),
                ('est'  = models.DateTimeField(db_column='est')),   
                ('rownum' = models.BigIntegerField(db_column='RowNum')),
                ],

            options={
                'db_table': 'Company',
                'managed': False,
            },
        ),
         migrations.CreateModel(
            name='Floor',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),  
                ('srccode'  = models.SmallIntegerField(db_column='SrcCode')),
                ('depcode' = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)),  
                ('depsrccode'  = models.SmallIntegerField(db_column='DepSrcCode')),
                ('name'  = models.CharField(db_column='Name')),   
                ('rownum' = models.BigIntegerField(db_column='RowNum')),
                ],

            options={
                'db_table': 'Floor',
                'managed': False,
            },
        ),
        migrations.CreateModel(
            name='Department',
            fields=[
                ('depcode' = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)),  
                ('depsrccode'  = models.SmallIntegerField(db_column='DepSrcCode')),
                ('name'  = models.CharField(db_column='Name')),   
                ('rownum' = models.BigIntegerField(db_column='RowNum')),
            ],
            options={
                'db_table': 'Department',
                'managed': False,
            },
        ),
    ]

为什么迁移文件中没有所有关系和主键约束?有什么我想念的吗?如何根据我的表关系构建模型?任何帮助都非常感谢。

requirements.txt

Django==2.1.7
django-composite-foreignkey==1.1.0
django-crispy-forms==1.7.2
django-extensions==2.1.6
django-filter==2.1.0
django-mssql==1.8
django-pyodbc-azure==2.1.0.0
djangorestframework==3.9.2
graphviz==0.10.1
Markdown==3.0.1
pydotplus==2.0.2
pyodbc==4.0.26
pyparsing==2.3.1
pytz==2018.9
pywin32==224
six==1.12.0

2 个答案:

答案 0 :(得分:0)

简短的答案是Django不像实体框架那样支持复合主键/索引组合。由于它本身不受支持,并且似乎是一项业务需求,因此我建议使用另一个ORM框架。如果您要使用Django,可以在这里阅读更多内容:

https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys

答案 1 :(得分:0)

您必须在元组中编写这样的元组,因为多行将是复合pk。 class Meta: manage = False unique_together = (('depcode', 'depsrccode'),) db_table = 'department'