Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:30:11+00:00 2026-05-22T15:30:11+00:00

With sqlalchemy, if one has an object with a orm relation attribute, and you

  • 0

With sqlalchemy, if one has an object with a orm relation attribute, and you create an instance of the object, and set the relation attribute, then your new object gets added to the session of the set relation. e.g.:

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, Session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class User(Base):
    __tablename__ = 'users'

    user_id = Column(Integer, primary_key=True)
    name = Column(String)

class Address(Base):
    __tablename__ = 'addresses'

    address_id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey(User.user_id),)
    address = Column(String)

    user = relationship(User, backref='addresses')


engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
session = Session(bind=engine)

bob = User()
bob.name = 'Bob'
session.add(bob)
session.commit()

bobs_addr = Address()
bobs_addr.user = bob
bobs_addr.address = "23 Bob's st."

# bobs_addr not added to the session

engine.echo = True
session.commit()

Output of running the above:

2011-05-23 12:07:23,461 INFO sqlalchemy.engine.base.Engine.0x...9a10 BEGIN (implicit)
2011-05-23 12:07:23,463 INFO sqlalchemy.engine.base.Engine.0x...9a10 SELECT users.user_id AS users_user_id, users.name AS users_name 
FROM users 
WHERE users.user_id = ?
2011-05-23 12:07:23,463 INFO sqlalchemy.engine.base.Engine.0x...9a10 (1,)
2011-05-23 12:07:23,466 INFO sqlalchemy.engine.base.Engine.0x...9a10 INSERT INTO addresses (user_id, address) VALUES (?, ?)
2011-05-23 12:07:23,466 INFO sqlalchemy.engine.base.Engine.0x...9a10 (1, "23 Bob's st.")
2011-05-23 12:07:23,467 INFO sqlalchemy.engine.base.Engine.0x...9a10 COMMIT

bobs_addr was not added to the session, so I would expect that it would not be committed to the db, however setting bobs_addr.user = bob caused bobs_addr to be added to the session. How do I stop this from happening?

There is a workaround that I am aware of, and that is to set bobs_addr.user_id = bob.user_id, but I would prefer to use the relation attr.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:30:12+00:00Added an answer on May 22, 2026 at 3:30 pm

    I think this can achieve what you want, but I have always found the default cascade='save-update, merge' to work best.

    user = relationship(User, backref=backref('addresses', cascade=''))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I select one or more random rows from a table using SQLAlchemy?
SQLAlchemy's DateTime type allows for a timezone=True argument to save a non-naive datetime object
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I'm using an ORM (SQLAlchemy, but my question is quite implementation-agnostic) to model a
I've got a sqlalchemy model that is set up like this: class Entry(Base): __tablename__
Here's the simplest description of my SQLAlchemy model: A Host object (subclass of Base),
I'm using SQLAlchemy 0.5rc, and I'd like to add an automatic filter to a
What is the sqlalchemy equivalent column type for 'money' and 'OID' column types in
How do I translate something like this into SQLAlchemy? select x - y as
How do I translate something like this into SQLAlchemy? SELECT (a * b) -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.