Opened 2 years ago

Closed 2 years ago

#29787 closed Bug (needsinfo)

ForeignKey to User with BigIntegerField id/pk gets created as 'integer' instead of 'bigint' field in postgres

Reported by: Andrew Badr Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Andrew Badr)

I ran into this bug when upgrading from 1.10 to 1.11. All my models have BigInteger ids. My test case creates a User model instance, then creates another model instance pointing to it via ForeignKey. On 1.10, this works fine, and inspecting the database shows the fk field as having type 'bigint' in postgres. On 1.11, the test fails, and inspecting the db shows the field as a regular 'integer'.

After some digging, I've found that this bug happens, oddly, at different commits for different models. For some models (??) it occurs starting in 45ded053b1f4320284aa5dac63052f6d1baefea9 (#27666). For others, b8a2f3c2d66aa15af4be745a576609b958a853c0 is the first bad commit (fix for #28305; comes between 1.11.7 and 1.11.8). The fact that the former has to do with migrations might be a clue.

I tried to repro with a minimal test case on a fresh project, but sadly was not able to recreate the bug. (It worked fine.) So there must be some other config in my project that's affecting this. If I have time I'll try to pin it down (already spent ~4 hours on this), but maybe someone who worked on the relevant patches can make an educated guess on what's happening?

Change History (6)

comment:1 Changed 2 years ago by Andrew Badr

Description: modified (diff)

comment:2 Changed 2 years ago by Andrew Badr

Description: modified (diff)

comment:3 Changed 2 years ago by Andrew Badr

Indeed, it looks like resetting my migrations (à la https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html) fixed the problem, so this has something to do with the migrations code. Maybe the data type for foreign keys is being determined too early in certain cases somehow?

comment:4 Changed 2 years ago by Tim Graham

It might be a duplicate of #25012.

comment:5 Changed 2 years ago by Andrew Badr

Agreed. My project worked with 1.10, though, so there's something tricky going on. My initial diagnosis of integer vs bigint being relevant was probably wrong-- I think this is all about foreign keys to migrated primary key types.

comment:6 Changed 2 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

Please reopen if you can provide a sample project that reproduces it.

Note: See TracTickets for help on using tickets.
Back to Top