Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create (or change) a model to use UUID/non-integer for its primary key #448

Open
lb- opened this issue Sep 19, 2023 · 2 comments
Open

Create (or change) a model to use UUID/non-integer for its primary key #448

lb- opened this issue Sep 19, 2023 · 2 comments

Comments

@lb-
Copy link
Member

lb- commented Sep 19, 2023

Problem

We often get bugs raised for models that use a non-integer primary key.

This creates a few issues with our code assumptions

  • Converting to a string for element ID usage or similar can have problems
  • Using some_instance.id will not work and needs to.be replaced with some_instance.pk
  • Assumptions about general ordering of thing in code can be false

Proposal

Assuming we can use a migration to modify existing models, that would be best as it avoids trying to think of new example data.

  1. breads.Country - Change to a non-integer primary key of a two letter string. E.g. 'AU', that's set on creation of any new entries, but will not be editable (read only but visible in the UI on edit).
  2. breads.BreadIngredient - Change to a non-integer primary key that's an auto-generated UUID (or BreadType). Not visible in the UI.

This way we are only changing models in one app and have a cross-section of UI interaction (choosers, snippet listing, history etc).

Related issues & PRs

Many are closed, but retesting this is often tricky and it would be great if we had a model ready to go. Plus, if problems like these are more visible we may find them earlier.

@lb- lb- added the demo data label Sep 19, 2023
@laymonage
Copy link
Member

Change to a non-integer primary key of a two letter string. E.g. 'AU' that's user editable.

Just to note – primary keys are read-only:

https://docs.djangoproject.com/en/4.2/ref/models/fields/#primary-key

The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one.

@lb-
Copy link
Member Author

lb- commented Sep 23, 2023

Ahh. Good point @laymonage - I'll fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants