0

I am making a classic bdd and I had a doubt expressed by another dev: To put it simply, I have 2 tables (with very very few updates), one of which has an FK (foreign key) on the other. There is an autoincrement on the PK (primary key).

With Django I make fixtures (basic filling) to fill these 2 tables and I put the following PK values:

id | codes | name | zone_id
1 | 13 | bla1 | 1
2 | 84 | bla2 | 2
3 | 06 | bla3 | 4

Simple, basic. But this colleague changed my fixtures because it's "better", maybe for convenience:

id | codes | name | zone_id
13 | 13 | bla1 | 1
84 | 84 | bla2 | 2
6 | 06 | bla3 | 4

As a result, the values of the PK no longer follow each other. Is this correct?, or should the auto-increment be removed?. For me it's completely absurd to have changed that.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
fabrice
  • 1,399
  • 1
  • 15
  • 27
  • Hi Fabrice, I guess your colleague wants to clean autoincrement select query so that it can be faster. Autoincrement uses sequences that each time you pick "nextval" of the sequence. I like autoincrement fields as its default for Django PK fields, but if the id sequence is not important and performance is important for your feature, it is ok to remove autoincrement from PK field. – Deniz Kaplan Feb 11 '22 at 08:33
  • Keep in mind that a PK hast one and only one requirement, **makes the row unique in the table**, nothing else. Auto incrementing virtually guarantees uniqueness. So the question becomes does the `code` have guaranteed uniqueness? If so then neither is better that the other,You could: just eliminate the id column; if the `code` is quartered to be unique. I would go with the auto incremented id. – Belayer Feb 12 '22 at 02:14
  • Hi, Thanks for these informations. I think i'll keep the auto_increment field, the performance is not important ; the table is tiny :). Have a nice day. – fabrice Feb 14 '22 at 10:11

0 Answers0