Fix bug in to_sql