When I did not know much about the question of database structure and relationship between tables, I used JSON or comma-separated data in a particular field in any table in the database.
In fact, I remember a specific case where I needed to relate multiple images to a particular post. These images saved a list in JSON
to a certain field called imagens
.
The structure was more or less like this
-----------------------------------------------
tabela
-----------------------------------------------
id | imagens | usuario_id
-----------------------------------------------
1 | ["image1.png", "image2.png"] | 55
2 | NULL | 56
But at a certain point in time when changes began to emerge in this system, I was harshly criticized by a programmer who knew more about database than I (and was kidding too) for doing so.
He explained that the data should look something like this:
--------------------------
tabela
--------------------------
id | usuario_id
--------------------------
1 | 55
2 | 56
----------------------------
imagens
----------------------------
post_id | url_imagem
1 | image1.png
1 | image2.png
Considering these two cases (VS serialization relationship saved in a field), can we say that, structurally speaking, using JSON
is bad practice?
I was criticized for using data in JSON
in a table field instead of relationship, but there is some case where I should / can use a serialization and save it in a database field, without this being considered a bad programming practice?