Python Pandas to_sql, how to create a table with a primary key?

Posted on

Question :

Python Pandas to_sql, how to create a table with a primary key?

I would like to create a MySQL table with Pandas’ to_sql function which has a primary key (it is usually kind of good to have a primary key in a mysql table) as so:

group_export.to_sql(con = db, name = config.table_group_export, if_exists = 'replace', flavor = 'mysql', index = False)

but this creates a table without any primary key, (or even without any index).

The documentation mentions the parameter ‘index_label’ which combined with the ‘index’ parameter could be used to create an index but doesn’t mention any option for primary keys.


Answer #1:

Disclaimer: this answer is more experimental then practical, but maybe worth mention.

I found that class has named argument key and if you assign it the name of the field then this field becomes the primary key:

Unfortunately you can’t just transfer this argument from DataFrame.to_sql() function. To use it you should:

  1. create instance

    engine = sa.create_engine('postgresql:///somedb')
    pandas_sql =, schema=None, flavor=None)
  2. define function analoguous to but with additional *kwargs argument which is passed to object created inside it (i’ve just copied original to_sql() method and added *kwargs):

    def to_sql_k(self, frame, name, if_exists='fail', index=True,
               index_label=None, schema=None, chunksize=None, dtype=None, **kwargs):
        if dtype is not None:
            from sqlalchemy.types import to_instance, TypeEngine
            for col, my_type in dtype.items():
                if not isinstance(to_instance(my_type), TypeEngine):
                    raise ValueError('The type of %s is not a SQLAlchemy '
                                     'type ' % col)
        table =, self, frame=frame, index=index,
                         if_exists=if_exists, index_label=index_label,
                         schema=schema, dtype=dtype, **kwargs)
  3. call this function with your SQLDatabase instance and the dataframe you want to save

    to_sql_k(pandas_sql, df2save, 'tmp',
            index=True, index_label='id', keys='id', if_exists='replace')

And we get something like

CREATE TABLE public.tmp
  id bigint NOT NULL DEFAULT nextval('tmp_id_seq'::regclass),

in the database.

PS You can of course monkey-patch DataFrame, io.SQLDatabase and io.to_sql() functions to use this workaround with convenience.

Answered By: krvkir

Answer #2:

Simply add the primary key after uploading the table with pandas.

group_export.to_sql(con=engine, name=example_table, if_exists='replace', 
                    flavor='mysql', index=False)

with engine.connect() as con:
    con.execute('ALTER TABLE `example_table` ADD PRIMARY KEY (`ID_column`);')
Answered By: tomp

Answer #3:

automap_base from sqlalchemy.ext.automap (tableNamesDict is a dict with only the Pandas tables):

metadata = MetaData()
metadata.reflect(db.engine, only=tableNamesDict.values())
Base = automap_base(metadata=metadata)

Which would have worked perfectly, except for one problem, automap requires the tables to have a primary key. Ok, no problem, I’m sure Pandas to_sql has a way to indicate the primary key… nope. This is where it gets a little hacky:

for df in dfs.keys():
    cols = dfs[df].columns
    cols = [str(col) for col in cols if 'id' in col.lower()]
    schema =[df],df, con=db.engine, keys=cols)
    db.engine.execute('DROP TABLE ' + df + ';')
    dfs[df].to_sql(df,con=db.engine, index=False, if_exists='append')

I iterate thru the dict of DataFrames, get a list of the columns to use for the primary key (i.e. those containing id), use get_schema to create the empty tables then append the DataFrame to the table.

Now that you have the models, you can explicitly name and use them (i.e. User = Base.classes.user) with session.query or create a dict of all the classes with something like this:

alchemyClassDict = {}
for t in Base.classes.keys():
    alchemyClassDict[t] = Base.classes[t]

And query with:

res = db.session.query(alchemyClassDict['user']).first()

Answer #4:

with engine.connect() as con:
    con.execute('ALTER TABLE for_import_ml ADD PRIMARY KEY ("ID");')

for_import_ml is a table name in the database.

Adding a slight variation to tomp’s answer (I would comment but don’t have enough reputation points).

I am using PGAdmin with Postgres (on Heroku) to check and it works.

Answered By: S.Doe_Dude

Leave a Reply

Your email address will not be published. Required fields are marked *