Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The SQL model cannot perform insertion into another table using queried data #1019

Open
writebai2 opened this issue Jul 17, 2024 · 2 comments

Comments

@writebai2
Copy link

writebai2 commented Jul 17, 2024

Issue Content

The SQL model cannot insert the queried data into the corresponding table of another database

engine = create_engine(str(settings.MYSQL_DATABASE_URL))
rds_engine = create_engine(str(settings.RDS_DATABASE_URI), echo=True)

def get_online_products():
    statement = (
        select(
            Tbl_Product
        )
        .limit(1)
    )

    with Session(engine) as session:
        products = session.exec(statement).all()
    return products

def insert_main():
    product_datas = get_online_products()
    with Session(rds_engine) as session:
        session.add_all(product_datas)
        session.commit()

if __name__ == "__main__":
    insert_main()

After execution:
PS D:\python\db_visualization> D:\python\myvenv\Scripts\python.exe "d:\python\db_visualization\back_main.py"

There is no response in the table after execution

@tholo
Copy link

tholo commented Jul 17, 2024

I forget if this is the case with plain SQLAlchemy -- if so, you may have to expunge() them (or all with expunge_all()) from the current session before adding it to a different one...?

@writebai2
Copy link
Author

writebai2 commented Jul 22, 2024

I forget if this is the case with plain SQLAlchemy -- if so, you may have to expunge() them (or all with expunge_all()) from the current session before adding it to a different one...?

I solved this problem by using mapping to insert data instead. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants