Using Primary key in two fields

159 views
Skip to first unread message

Rakhee Menon

unread,
Oct 17, 2018, 5:55:48 AM10/17/18
to Django users
Hi Everyone,

I have a scenario where one field needs to be a primary key and another field needs to be an AutoField....and Autofield requires a condition primary_key = true

I get this error 
django_reports.MstCompositionFm.makeid: (fields.E100) AutoFields must set primary_key=True.
django_reports.MstSalarystructure: (models.E026) The model cannot have more than one field with 'primary_key=True'

This is my case:

icatid = models.BigIntegerField(db_column='ICatID', primary_key=True)
makedate = models.DateTimeField(db_column='MakeDate')
revdate = models.DateTimeField(db_column='RevDate', blank=True, null=True)
makeid = models.BigAutoField(db_column='MakeId', primary_key=False)

It would be of great help if anyone could figure out whats the solution

Thanks in Advance,
Rakhee

Ігор Магур

unread,
Oct 17, 2018, 7:32:45 AM10/17/18
to django...@googlegroups.com
As I saw earlier, Django does not allow you to have two rows as autofield, you can make your own save method for model. Ex. get first of YourModel.objects invert sorted by wanted field, add to it's value 1 and save

ср, 17 жовт. 2018, 08:55 користувач Rakhee Menon <menonr...@gmail.com> пише:
--
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...@googlegroups.com.
To post to this group, send email to django...@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/341d511d-3c95-42b9-a22f-a5fa0c331da5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michal Petrucha

unread,
Oct 17, 2018, 11:56:36 AM10/17/18
to Django users
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
Hi Rakhee,

By definition, there may only be one primary key per table – primary
key is the unique key that has been chosen as the primary, all other
unique keys are secondary. AutoFields have a requirement that if they
are present in a table, they have to be the primary key of that table,
because that's how auto-increment columns work in certain databases.

What exactly is it that you're after? Do you just want both icatid and
makeid to be unique? If so, set primary_key=True on the AutoField, and
for icatid, just use unique=True instead. That will tell the database
that icatid is a secondary key.

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJbxyMqAAoJEHA7T/IPM/klo8AP/i83PGOzi7+rwqc566VqdYT9
DY5SF9Vh/NAhCAxNVaC369yXRr2/L2EE0T3mDe9hMv/fSbSp00D25kFwsdLDC5TA
+tIzLN53777mAWFzTIjRhzQuc8usNRvHNcsbfJsaurbX1vm/EObdfjtnTlUzK2z2
KwieFMdbpLOicM7YDHGAGj/wF341blFhu0uDyeHQ6rcrEytaAtcMfPLEE92Ts3YR
qpxGPUwzAbhLOV5xcMsPgT6lnqE8JNxnBjA6DAqxoVk02c2tDoqEiPb71VvPzKdC
OEDxlN4veDSBciuP/ngNHG3kKL7EB5Ce60lo4pAPABhxx8YB6SVJ5gisP6s69j0w
xodELja8zVcrNi0uE0LgnvwS64XG1U5vPSSX6HJzZhozPP2aoP+NZEWcV94IX51O
4BfjWFn45LKLGy+/KENlgvPKMoKSYd9tuTcDFKDmLDb62JIq2ylCFUfj5kdxdZSO
8uMf9ZexQ9Gz604Hbl/xDneUV8k/xu5yfrHk6BX0YRtBxtqIF8E8ZYviqagp2Dw2
Z/zdNSMIGD+j3VxaY7ixGwcHq2svHY/g6AiqUXlA4NuM6KGLO+aWRcjlErryp4gL
q5Kuz5+4UCczpj/qKuNym/6t7BKn51pzX97gZghIVM9A2R5guOW30TB5PCJvgzDN
nOR2s5TXmuVEUdfiOU7d
=D1aU
-----END PGP SIGNATURE-----

Vishvajit Pathak

unread,
Oct 20, 2018, 10:24:28 AM10/20/18
to Django users
Hi Rakhee,
Could you try following ? :

icatid = models.BigIntegerField(db_column='ICatID') makedate = models.DateTimeField(db_column='MakeDate') revdate = models.DateTimeField(db_column='RevDate', blank=True, null=True) makeid = models.BigAutoField(db_column='MakeId') class Meta: unique_together = (('icatid', 'makeid'),)
Reply all
Reply to author
Forward
0 new messages