You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating tables, SQLAlchemy will automatically set AUTO_INCREMENT on the first Integer primary key column which is not marked as a foreign key[...]
The id 0 is somehow not even inserted! If one executes this code multiple times then MySQL will keep adding ids above 200 for some reason. I don't really understand MySQL's behavior but this is definitely not the desired behavior.
importpandasaspdfrompangresimportupsertfromsqlalchemyimportcreate_engine, text# config, change connection string accordingly but it must be MySQLengine=create_engine('mysql+pymysql://username:password@localhost:3306/db')
# upsert first df using pangres which creates the table automaticallydf1=pd.DataFrame({'id':[0, 1], 'name':['foo', 'bar']}).set_index('id')
upsert(con=engine, df=df1, table_name='test_pk_mysql', if_row_exists='update')
# upsert second dfdf2=pd.DataFrame({'id':[100, 200], 'name':['baz', 'qux']}).set_index('id')
upsert(con=engine, df=df2, table_name='test_pk_mysql', if_row_exists='update')
# read df backwithengine.connect() asconnection:
print(pd.read_sql(text('SELECT *FROM test_pk_mysql;'), con=connection, index_col='id').to_markdown())
id
name
1
bar
100
baz
200
qux
The text was updated successfully, but these errors were encountered:
We need to disable this!!
Code to reproduce the problem
The id
0
is somehow not even inserted! If one executes this code multiple times then MySQL will keep adding ids above 200 for some reason. I don't really understand MySQL's behavior but this is definitely not the desired behavior.The text was updated successfully, but these errors were encountered: