Using a UUID as a primary key in Django models (generic relations impact)

前端 未结 6 741
旧巷少年郎
旧巷少年郎 2020-12-02 06:52

For a number of reasons^, I\'d like to use a UUID as a primary key in some of my Django models. If I do so, will I still be able to use outside apps like \"contrib.comments\

6条回答
  •  清歌不尽
    2020-12-02 07:45

    A UUID primary key will cause problems not only with generic relations, but with efficiency in general: every foreign key will be significantly more expensive—both to store, and to join on—than a machine word.

    However, nothing requires the UUID to be the primary key: just make it a secondary key, by supplementing your model with a uuid field with unique=True. Use the implicit primary key as normal (internal to your system), and use the UUID as your external identifier.

提交回复
热议问题