Skip to content

Bulk upsert in Mysql dialect using on_duplicate_key_update #9328

Discussion options

You must be logged in to vote

Thanks for educating me here, it works! I also revised my example code to add setup and demonstrate upsert. Please see below.

import sqlalchemy as db
import sqlalchemy.dialects.mysql as mysql
from sqlalchemy import delete, select, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "foo"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(30))


engine = db.create_engine('mysql+mysqlconnector://USER-NAME-HERE:PASS-WORD-HERE@localhost/SCHEMA-NAME-HERE')
conn = engine.connect()

# setup step 0 - ensure the table exists
Base().metadata.create_all(b…

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@CaselIT
Comment options

Comment options

You must be logged in to vote
1 reply
@CaselIT
Comment options

Comment options

You must be logged in to vote
4 replies
@benthecoder
Comment options

@CaselIT
Comment options

@benthecoder
Comment options

@CaselIT
Comment options

Answer selected by CaselIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants