I did some Google searches for you.
There’s still an open ticket for it, open for the past 12 years:
https://code.djangoproject.com/ticket/373

There’s plenty of people asking the same question.  Here’s one on Stack 
Overflow:
https://stackoverflow.com/questions/16800375/how-can-set-two-field-primary-key-for-my-models-in-django

And it looks like there’s an app for it that hasn’t been updated since 2014:
https://github.com/simone/django-compositekey

In short, though, it looks like you’re doing everything correctly.  You could 
always overwrite the save method and the refresh_from_db method for your use 
case.

If you can change the table at all, I would add an ID column and skip 
specifying the primary_key column in the model.  You could also make a new view 
inside the database that is an interface to the foreign table and write your 
trigger SQL in your database system to handle inserts, updates, and deletes.


From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Kevin Olbrich
Sent: Wednesday, August 1, 2018 1:01 PM
To: Django users
Subject: External DB layout, two column as PK, modification not possible

Hi!

I got the following table definition on inspect db:
class KevinExample(models.Model):
    code = models.CharField(db_column='Code', primary_key=True, max_length=50)  
# Field name made lowercase.
    lineid = models.IntegerField(db_column='LineId')  # Field name made 
lowercase.
    object = models.CharField(db_column='Object', max_length=20, blank=True, 
null=True)  # Field name made lowercase.
    loginst = models.IntegerField(db_column='LogInst', blank=True, null=True)  
# Field name made lowercase.
    u_contrattr = models.ForeignKey('KevinExpAttr', models.DO_NOTHING, 
db_column='U_contrattr', max_length=50, blank=True, null=True)  # Field name 
made lowercase.
    u_attrlinedata = models.CharField(db_column='U_attrlinedata', 
max_length=50)  # Field name made lowercase.
    class Meta:
        managed = False
        db_table = '@KEVIN_EXAMPLE'
        unique_together = (('code', 'lineid'),)

Inserting new rows by using the constructor and force_insert works fine but 
refresh_from_db() as well as save() is not possible (duplicate key constraint).
Problem is, code + lineid is PK and I have to keep it like this (foreign table).

Is there a workaround for this? Is it still impossible to define such PK on 
latest Django?

Kind regards
Kevin
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/857e15d5-e46b-4f1e-b1f3-e5c24b1f3bd8%40googlegroups.com<https://groups.google.com/d/msgid/django-users/857e15d5-e46b-4f1e-b1f3-e5c24b1f3bd8%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/368d673d8b234735b47c03ab7356b834%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to