G
G
gpm73152019-07-25 14:53:21
Database design
gpm7315, 2019-07-25 14:53:21

Is an array a string in a database bad?

Hey! Let's say there are several images for each product in the store. What if, instead of an additional table, a list of these images is stored in the database in one line, and in the application it is "split" with a split? Is this a good decision or is it better not to do this?
In DB:
"/imgs/products/147832/01.jpg;/imgs/products/147832/02.jpg;/imgs/products/147832/03.jpg"
In application (SQLAlchemy):

class Product(Base):
   ...
   _imgs = Column("imgs", Text())

   @property
   def imgs(self):
       return self._imgs.split(";")

   @imgs.setter
   def imgs(self, imgs):
       self._imgs = ";".join(imgs)

PS: I know that postgres has an array type

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton R., 2019-07-25
@anton_reut

Yes, bad. It seems to me much easier to create a separate table with id, product_id and image columns, then join, than to fence splits or some other processors (potentially a bottleneck where jambs and errors are likely).

I
Ivan Shumov, 2019-07-25
@inoise

In some cases, this can and should be done, but these are very narrowly specific cases and optimization. Lead to 3NF and don't suffer

E
Eugene Wolf, 2019-07-25
@Wolfnsex

What if, instead of an additional table, a list of these images is stored in the database in one line, and in the application it is "split" with a split?
If you have MySQL - there is support for JSON data type, which is ideal for such purposes.
Is this a good decision or is it better not to do this?
Well, at least this solution has the right to life if you do not plan to process this data separately (for example, to search for it). But its meaning is not clear to me personally.
PS: I know that postgres has an array type
PostgreSQL also has a JSONB type, which allows you to fully index fields in JSON format ... But I still don’t see much point in such a solution, rather the opposite.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question