5
When I didn’t know much about the question of database structure and relationship between tables, I used to use JSON or comma-separated data in a given field of any table in the database.
In fact, I remember a specific case where I needed to relate several images to a particular post. These images I saved a list on JSON
in 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
However, at a certain point where changes in this system began to appear, I was harshly criticized by a programmer who knew more about databases than I (and was also mocked) for having done so.
He explained to me 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), we can state that, structurally speaking, using the JSON
is a bad practice?
I was criticized for using data in JSON
in a table field instead of a 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?
I believe that if you have unrelated data that can be saved to a database, such as a user’s custom settings for example, you can choose to
json
unless you will need details about this update.– Erlon Charles
Using the first example you posted would it be possible to define date and time of inclusion of each of the images? This may be important information for some systems, but if these details make no difference to these saved data, I see no harm in saving
json
– Erlon Charles
Yeah, the problem is that my boss later talked about making "photo album" with these images. Then it would have to have the image date yes :(. These future implementations detonate the bank’s code and structure!
– Wallace Maxters
Okay, ultimately it depends on how this data will be used. If I save for example a
json
with the theme settings for a system made by a user and I just need to know what the final configuration of the change is like,json
by usiário can facilitate a lot of things.– Erlon Charles
Therefore, it is only bad practice if the structure created impairs the performance or development of its solution.
– Erlon Charles
I disagree in the sense that the evolution of the comic book scheme is a boring thing, yes, but necessary: trying to escape it using "shortcuts" has a good chance to bring headache after... An exception is when the "variable" data will not be used in the database at all, and there is not much prediction of what structure they will have. A real example that occurred in a system of mine are the Preferences of the User Interface - Each user has their JSON with their preferences, and whenever the UI changes the set of preferences it can change. But since none of this has anything to do with the BD, a JSON is enough.
– mgibsonbr