I have a table with column name "collation" whose schema I cannot change.
I generated some data I want to insert into this table using the following code snippet:
from sqlalchemy.dialects import postgresql
data = [{"collation": "foo"}, {"collation":"bar"}]
stmt = postgresql.insert(table).values(data)
with engine.connect() as conn:
conn.execute(stmt.compile(dialect=postgresql.dialect()))
conn.commit()
I get the following error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "collation"
LINE 1: ... (id, column_name, common_data_type, default_val, collation,...
^
I have tried replacing the column name with quoted_string("collation",True)
but I still get the same error.
How can I get sqlalchemy to generate the insert query with quotes around the column collation
?
2
Answers
This approach, manually constructing the queries works, know that you quote all columns in the insert statement not just keywords.
Passing
quote=True
toColumn()
seems to work. I’m not sure where your schema is coming from but it seems to work for the ORMclass
and also with aTable()
definition.