Skip to content

to_sql double quoting in create index #9393

Closed
@dashesy

Description

@dashesy

I have table names that include _, . and -, up until Pandas .14 I used to quote the table name like this:

    def quote_identifier(s, errors="ignore"):
        encodable = s.encode("utf-8", errors).decode("utf-8")

        nul_index = encodable.find("\x00")

        if nul_index >= 0:
            error = UnicodeEncodeError("NUL-terminated utf-8", encodable,
                                       nul_index, nul_index + 1, "NUL not allowed")
            error_handler = codecs.lookup_error(errors)
            replacement, _ = error_handler(error)
            encodable = encodable.replace("\x00", replacement)

        return "\"" + encodable.replace('"', '""') + "\""

then call:

    pd_sql.to_sql(group, quote_identifier(name), con, if_exists='replace')

But since 0.15 if I do not quote CREATE TABLE (or DROP TABLE) will give me an error:

    OperationalError: near "-": syntax error

And if I do quote CREATE INDEX will give me an error:

    OperationalError: near ""_b.m_test_01-30"": syntax error

The problem is that in CREATE INDEX ix_{tbl}_{cnames} ON {tbl} ({cnames_br}) CREATE INDEX thinks it is the only one trying to prepend a namespace with _ and thus does not check if {tbl} is already quoted.

The proper fix should do what quote_identifier does with " and escape them, or just use quote_identifier and then prepend ix_.

This is the query that it tries and gives error:

CREATE INDEX ix_"_b.m_test_01-30"_index ON "_b.m_test_01-30" ([index])

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions