T
T
tteqwe2020-03-16 23:00:56
Flask
tteqwe, 2020-03-16 23:00:56

How to insert data using sqlalshemy from an array into a database?

Such a question: there are two tables with a one-to-many relationship. The values ​​of one of the tables may come with sets of values. How can this data be inserted into the database? First I get the data from the form

if request.method == 'POST':
            app_name_get = request.form['app_name']
            email_get = request.form['email']

Next, I get data, which can be a lot
for name_file in request.files:
                    file = request.files[name_file]
                    addFile = (Hotel(name_file=file.name, data_file=file.read()))
                    addFiles.append(addFile)

After that, it remains only to throw the data into the database.
connect = engine_mart.connect()
                sessions = session_mart()
                new_feedback_message = Location(app_name=app_name_get, email=email_get, phone=phone_get,
                                                        bug_or_sentence=bug_or_sent_get, message=message_get,
                                                        user_id=user_id_get)

                new_feedback_message.hotels.append(addFiles)
                sessions.add(new_feedback_message)
                sessions.commit()   ```

How can I insert this data so that one new_feedback_mesage entry has several addFiles? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseny, 2020-03-17
Matytsyn @ArsenyMatytsyn

For multiple connections you need:
1. The table to which you bind something.
2. A table that stores links.
3. A table in which you store what you want to bind.
Alternatively, you could create a media library entirely managed by your application that would contain file links, titles, feedback, and maybe extension, permissions, etc. Or just links to a file, thus having received an array with files, first of all you need to save them to disk. Not in DB. In the database store only links to files. And sorting through each of them to form an entity that you will bind to your feedback entity.
In addition, you can store them as a PickleType, or form links as a string that you can parse and rewrite.
IMHO, the first option is more optimal, however, it creates some difficulties when transferring the database, for example. I mean there are records in the database, but there may not be files, and deleting files will also have to be manually written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question