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

MySQL creating undesired auto incrementing primary keys #56

Closed
ThibTrip opened this issue Jan 17, 2022 · 0 comments
Closed

MySQL creating undesired auto incrementing primary keys #56

ThibTrip opened this issue Jan 17, 2022 · 0 comments

Comments

@ThibTrip
Copy link
Owner

When creating tables, SQLAlchemy will automatically set AUTO_INCREMENT on the first Integer primary key column which is not marked as a foreign key[...]

Source

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.

import pandas as pd
from pangres import upsert
from sqlalchemy import create_engine, text

# config, change connection string accordingly but it must be MySQL
engine = create_engine('mysql+pymysql://username:password@localhost:3306/db')

# upsert first df using pangres which creates the table automatically
df1 = 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 df
df2 = 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 back
with engine.connect() as connection:
    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
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

1 participant