Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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).
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
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 typePostgreSQL 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 questionAsk a Question
731 491 924 answers to any question