Ask Your Question
0

How can related data be inserted or updated in Flask/SQLAlchemy?

asked 2022-04-24 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-29 02:00:00 +0000

david gravatar image

To insert or update related data in Flask/SQLAlchemy, you can follow these steps:

  1. Create models for your tables using SQLalchemy.
  2. Define the relationships between the tables.
  3. Create instances of the parent table (with primary key values) and then add child table instances to it.
  4. Use session.add() to add the newly created instances to the session.
  5. Use session.commit() to commit the changes to the database.
  6. Use session.merge() to update the related data based on changes to the objects made outside of the session.

For example, consider a scenario where you have two tables - User and Address, with a one-to-many relationship (one user can have multiple addresses). Here are the steps to insert data into both tables:

  1. Define the models for User and Address:
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from app import db

class User(db.Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    age = Column(Integer)
    addresses = relationship("Address", back_populates="user")

class Address(db.Model):
    id = Column(Integer, primary_key=True)
    street = Column(String(50))
    city = Column(String(50))
    state = Column(String(2))
    zip = Column(String(10))
    user_id = Column(Integer, ForeignKey("user.id"))
    user = relationship("User", back_populates="addresses")
  1. Create an instance of User and multiple instances of Address associated with that User:
user = User(name="John", age=30)
address1 = Address(street="123 Main St", city="Anytown", state="NY", zip="12345", user=user)
address2 = Address(street="456 Oak Ave", city="Anytown", state="NY", zip="12345", user=user)
  1. Add the newly created instances to the session:
db.session.add(user)
db.session.add(address1)
db.session.add(address2)
  1. Commit the changes to the database:
db.session.commit()

This will insert the new data into the database.

To update related data, you can modify the instances outside of the session and then use session.merge() to update the changes in the database:

# Retrieve the user instance from the database
user = db.session.query(User).filter_by(name="John").one()
# Update the name of the user
user.name = "Johnny"
# Update the zip code of the first address
user.addresses[0].zip = "54321"

# Merge the changes into the session and commit
db.session.merge(user)
db.session.commit()

This will update the related data in the database.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-24 11:00:00 +0000

Seen: 8 times

Last updated: Jan 29 '22