D
D
Dos2020-09-14 01:03:34
Database design
Dos, 2020-09-14 01:03:34

UUID or Alias ​​as ID?

Hello. There is a slight misunderstanding of how best to proceed with the formation of the ID. Let me explain with a simple example to make it clear.

There is a blog module. He has posts. Each post has a category.

At first, there were not many categories and we decided to make these categories constants:

const CATEGORY_BOOK = “book”;
const CATEGORY_EDUCATION = “education”;

Gradually, the categories begin to grow and the way to add categories through the code is no longer satisfactory. The solution is to move the addition of categories through the database.

But there is a small problem here. The generated UUID is used as an ID in the project.

1. If we transfer the categories from the code to the database and use the UUID as the ID, then we will have to create the following structure:
id (uuid), alias (from a constant), title.

In this case, categories in posts have a link not by ID, but by alias. ID is not really needed. Or you need to replace alias in posts with a newly generated ID. How is it better?

2. Use alias categories from constants as IDs. And then we get such a structure without UUID:
id (from a constant), title

Tell me what to do in such cases when at first this was a constant and the data was saved from the constant, and then they decided to transfer it to the database. How to be? How is it better?

Thanks for the help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2020-09-14
@pro-dev

The second option has no problems if directory management is controlled and there are no delete operations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question