[PYTHON] How to get parent id with sqlalchemy

I want to automatically number the parent_id

It is required when you want to enter information on the parent-child relationship of data in the database. For example, a hotel may have multiple rooms tied to it, or a service may have multiple plans tied to it.

The important points are the following two points.

  1. It is necessary to get the ID of the parent data before storing the child data.
  2. If an error occurs when storing child data, it is necessary to roll back to before storing the parent data.

Use session.flush () for sqlalchemy

session.add(parent_record) #Transaction registration
session.fluhs() #Registered in DB (not saved)
parent_id = parent_record.id #Get parent ID

At the time of session.add (record), the ID of the parent data has not been assigned, so it cannot be obtained. By doing session.flush (), the parent ID will be assigned and you will be able to get it.

Why not use session.commit ()

session.add(parent_record)
session.commit() #Registered in DB (saved)
parent_id = parent_record.id #Get parent ID

It can be stored in the database with session.commit () and can be obtained because the parent ID is assigned. However, since the rollback mentioned in important point 2 cannot be performed, if storage fails with child data, a situation will occur in which only the parent data is stored, and it takes time and effort to register the child data. I will end up. (For example, manually assigning a parent ID) If possible, the insert will be more efficient if the parent and child data are committed in the same session.

Recommended Posts

How to get parent id with sqlalchemy
How to update with SQLAlchemy?
How to Alter with SQLAlchemy?
How to Delete with SQLAlchemy?
How to get more than 1000 data with SQLAlchemy + MySQLdb
How to get started with Scrapy
How to get started with Python
How to get started with Django
How to INNER JOIN with SQLAlchemy
How to use SQLAlchemy / Connect with aiomysql
How to get started with laravel (Linux)
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to get results from id in Celery
Get table dynamically with sqlalchemy
How to get a logged-in user with Django's forms.py
How to convert a class object to a dictionary with SQLAlchemy
Introduction to RDB with sqlalchemy Ⅰ
How to cast with Theano
Get thread ID with python
How to get mouse wheel verdict with Python curses
How to separate strings with','
How to RDP with Fedora31
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to cancel RT with tweepy
How to handle session in SQLAlchemy
Python: How to use async with
Link to get started with python
Connect to multiple databases with SQLAlchemy
Get AWS account ID with boto3
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
How to get the Python version
Introduction to RDB with sqlalchemy II
How to deal with DistributionNotFound errors
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to install mysql-connector with pip3
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to get the directory where the EXE built with Pyinstaller exists
Here's a brief summary of how to get started with Django
How to use Visual Recognition to get LINE ID from a girl
How to get all traffic through VPN with OpenVPN on Linux
[Python] How to get a value with a key other than value with Enum
How to do arithmetic with Django template
Step notes to get started with django
[Blender] How to set shape_key with script
How to title multiple figures with matplotlib
How to get rid of long comprehensions
How to add a package with PyCharm
How to get IP when Tornado + nginx
I tried to get started with Hy
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)