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

ENH: Add 'truncate' option for if_exists arg for DataFrame.to_sql method #52961

Conversation

lzieglerpenn
Copy link

@lzieglerpenn lzieglerpenn commented Apr 27, 2023

This is my first ever PR on an open source project. This one seemed pretty small & straight forward so I gave it go. Just merged main into the branch so it is conflict free. Please let me know if there's anything else I can do to help get this merged!

See issue #37210 for more context but this essentially gives a user the option to truncate an existing table before inserting new rows into it via the DataFrame.to_sql method.

@lzieglerpenn lzieglerpenn marked this pull request as ready for review April 28, 2023 16:43
@iBoxerman
Copy link

Amazing!
I was wondering why truncate isn’t supported.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, but it's preferred to use a sqlalchemy function to truncate instead of running raw SQL since it's not guaranteed for the SQL to be database dialect agnostic

@lzieglerpenn
Copy link
Author

Thanks for the PR, but it's preferred to use a sqlalchemy function to truncate instead of running raw SQL since it's not guaranteed for the SQL to be database dialect agnostic

Thanks @mroeschke ! I couldn't find anything in the SQLalchemy docs for truncate specifically but they do have .delete() which would have the same end result of keeping the table but removing all the existing rows. Would that be acceptable function to use for the truncate option?

@mroeschke
Copy link
Member

I suppose delete would work, but possible renaming the option to delete_rows would be more appropriate

@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

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

Successfully merging this pull request may close these issues.

ENH: DataFrame.to_sql with if_exists='replace' should do truncate table instead of drop table
3 participants