R
R
REDkiy2020-04-05 22:18:42
Python
REDkiy, 2020-04-05 22:18:42

How to store a list of strings in a single field in a database?

In the database, there is a table of entities whose primary key is a string (Table No. 1). There is also a table (Table No. 2) that stores entities that must be associated with some entities from Table No. 1.

It is enough for entities from Table No. 2 to have a list of primary keys from Table No. 1, the number of keys can vary within different limits.

At the moment, there are about 12 million records in Table No. 2.
What you don't want:

  • Create a many-to-many relationship
  • Create a text field with a list of primary keys.

Used by SQLAlchemy, PostgreSQL and Python. The ideal would be to take a Python list of strings and, with minimal transformations, put and retrieve it from the database.

What is the appropriate solution in such conditions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-04-05
@REDkiy

It would be more correct to make a 3rd table linking the identifiers of the first 2 tables than to fence some text or json field with many identifiers in the SQL database.
The condition is not entirely clear.

What I don't want:
Create a many-to-many relationship
what was meant by this and why exactly did not want to. Moreover, judging by the description, a many-to-many relationship is required, judging by
It is enough for entities from Table No. 2 to have a list of primary keys from Table No. 1, the number of keys can vary within different limits.

As a last resort, if you only need a 1-to-many relationship (Table 1 IDs will not be duplicated in Table 2), you can add a field with the ID from Table 2 to Table 1. Less work, but less generic.

A
Antonio Solo, 2020-04-05
@solotony

json, pickle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question