bakerydemo icon indicating copy to clipboard operation
bakerydemo copied to clipboard

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

Open lb- opened this issue 2 years ago • 2 comments

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.

  • https://github.com/wagtail/wagtail/issues/10856
  • https://github.com/wagtail/wagtail/issues/8242 (potentially)
  • https://github.com/wagtail/wagtail/issues/8563
  • https://github.com/wagtail/wagtail/issues/7235
  • https://github.com/wagtail/wagtail/issues/7206
  • https://github.com/wagtail/wagtail/issues/6512
  • https://github.com/wagtail/wagtail/issues/2347
  • https://github.com/wagtail/wagtail/pull/7208
  • https://github.com/wagtail/wagtail/pull/4166
  • https://github.com/wagtail/wagtail/pull/10900
  • https://github.com/wagtail/wagtail/issues/7282

lb- avatar Sep 19 '23 20:09 lb-

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.

laymonage avatar Sep 21 '23 08:09 laymonage

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

lb- avatar Sep 23 '23 00:09 lb-