1
votes

I just start new Django project with existing MSSQL (not mysql) database. In database I have four table which all are in relationship with together. I build the model for existing table bypython manage.py inspectdb (table _name). but it doesn’t gives any relation neither foreign key nor OneToOne. So I update the models.py as per table relationship.

Note that each table has composite primary key and composite foreign key. I define composite primary key with unique_togather=() and for composite foreign key by using Django third party library Django-composite-foreignkey module.

But when I tried to migrate .it doesn’t seem to establish the relationship

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'

I add all composite foreign and primary key constrain after auto create models from inspectdb. My initial migration file as per below

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,
            },
        ),
    ]

Why in migration file doesn’t have all relationship and primary key constrain.? Is there anything that I am missing.? how can i build the models as per my table relations? Any help much appreciate.

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

2 Answers

0
votes

The short answer is that Django does not support composite primary keys/Index combos like entity framework does. Since it's not natively supported and it appears to be a business requirement, I might suggest using another ORM framework. If you're bound to Django, you can read more here:

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

0
votes

You have to write like this tuple inside tuple because of multiple row will be composite pk. class Meta: manage = False unique_together = (('depcode', 'depsrccode'),) db_table = 'department'