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

  • Home
  • SEARCH
  • 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 7537227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:47:34+00:00 2026-05-30T06:47:34+00:00

I am trying to move an object from one database into another. The mappings

  • 0

I am trying to move an object from one database into another. The mappings are the same but the tables are different. This is a merging tool where data from an old database need to be imported into a new one. Still, I think I am missing something fundamental about SQLAlchemy here. What is it?

from sqlalchemy import Column, Float, String, Enum
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import orm
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

DeclarativeBase = declarative_base()
class Datum (DeclarativeBase):
    __tablename__ = "xUnitTestData"
    Key = Column(String, primary_key=True)
    Value = Column(Float)
    def __init__ (self, k, v):
        self.Key = k
        self.Value = v

src_engine = create_engine('sqlite:///:memory:', echo=False)
dst_engine = create_engine('sqlite:///:memory:', echo=False)
DeclarativeBase.metadata.create_all(src_engine)
DeclarativeBase.metadata.create_all(dst_engine)
SessionSRC = sessionmaker(bind=src_engine)
SessionDST = sessionmaker(bind=dst_engine)
item = Datum('eek', 666)

session1 = SessionSRC()
session1.add(item)
session1.commit()
session1.close()

session2 = SessionDST()
session2.add(item)
session2.commit()
print item in session2 # >>> True
print session2.query(Datum).all() # >>> []
session2.close()
  • 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-30T06:47:35+00:00Added an answer on May 30, 2026 at 6:47 am

    I’m not really aware about what happens under the hood, but in the ORM pattern an object matches to a particular row in a particular table. If you try to add the same object to two different tables in two different databases, that doesn’t sound like a good practice even if the table definition is exactly the same.

    What I’d do to workaround this problem is just create a new object that is a copy of the original object and add it to the database:

    session1 = SessionSRC()
    session1.add(item)
    session1.commit()
    
    new_item = Datum(item.Key, item.Value)
    session2 = SessionDST()
    session2.add(new_item)
    session2.commit()
    
    print new_item in session2 # >>> True
    print session2.query(Datum).all() # >>> [<__main__.Datum object at 0x.......>]
    session2.close()
    session1.close()
    

    Note that session1 isn’t closed immediately to be able to read the original object attributes while creating the new object.

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

Sidebar

Related Questions

Ok so I'm trying to move items from one listbox to another by using
I am trying to move email from one pst to another. Sample code from
I'm trying to drag and drop an image from one spot on the canvas
I have created one service. Now I am trying to access database in onstart()
I am trying to ease (gradually move) the rotation of an object to an
Am trying to move an antique C++ code base from gcc 3.2 to gcc
I am trying to move the following function listView_SelectionChanged away from code-behind and handle
I am reading from database a large collections of this type List<Rows<Long,String,ByteBuffer>> I then
Hi I'm struggling do refine/refactoring a domain model and trying to move logic from
I am trying to make an Object move to a Cube wich is stored

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.