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 8466867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:28:15+00:00 2026-06-10T15:28:15+00:00

I need to attach an object to session in such a way that it

  • 0

I need to attach an object to session in such a way that it will not differ from one persisted in db. (Easier to explain it with code):

session.query(type(some_object)).filter_by(id=some_object.id).one()

Is there more proper way to do that?
session.add(some_object) doesn’t work since an entity with such id can already be attached to this session, and object = session.merge(some_object) doesn’t work for me because it translates state from detached copy (if i make object.name=’asdfasdf’ these changes will be pending after merging object)

EDIT:

I found a bit less ugly way:

some_object = session.merge(some_object)
session.refresh(some_object)

But is there a way todo this in one call?

  • 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-06-10T15:28:16+00:00Added an answer on June 10, 2026 at 3:28 pm

    I need to attach an object to session in such a way that it will not differ from one persisted in db.

    “will not differ from DB” pretty much means you’re looking to load it, so query it. You might want to consider that the object might already be present in that target session. so your approach with query(type(object)) is probably the most direct, though you can use get() to hit the primary key directly, and populate_existing() to guarantee that state which already exists in the session is overwritten:

    session.query(type(some_object)).populate_existing().get(some_object.id)
    

    the above calls down to almost the same codepaths that refresh() does. The merge/refresh approach you have works too but emits at least two SELECT calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to attach text labels to objects that are spread randomly across the
I'm busy writing a BHO(Browser Helper Object) in C# and I need to attach
I'm using Rails 3 to create a project that will need a model called
I need to attach an event handler to an object, and I placed this
I'm writing a Powershell script that will extract a set of data files from
Following on from this question I now have code that can attach to a
I need to attach EF object (previously detached) to a new ObjectContext. The problem
i need to attach the document object to move div inside another div, here
I have a class which derived from wx.frame and need to attach it to
I need to attach my database, that resided in the MyData directory of my

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.