3
votes

I'm trying to use SqlAlchemy with PyOdbc using the Microsoft ODBC Driver for Linux, but when I use the session.query() function, I get the following error:

pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'TableName'. (208) (SQLExecDirectW)")

So I saw that the query created by orm had square brackets on TableName.

SELECT 
    [mb.Tecnico].cod_tecnico AS [mb.Tecnico_cod_tecnico], 
    [mb.Tecnico].nome AS [mb.Tecnico_nome], 
    [mb.Tecnico].login AS [mb.Tecnico_login], 
    [mb.Tecnico].senha AS [mb.Tecnico_senha] 
FROM 
    [mb.Tecnico] 
ORDER BY 
    [mb.Tecnico].nome
1
Can you show the raw query? - Alex K.
Is your table called actually named TableName? - SqlZim
[SQL: 'SELECT [mb.Tecnico].cod_tecnico AS [mb.Tecnico_cod_tecnico], [mb.Tecnico].nome AS [mb.Tecnico_nome], [mb.Tecnico].login AS [mb.Tecnico_login], [mb.Tecnico].senha AS [mb.Tecnico_senha] \nFROM [mb.Tecnico] ORDER BY [mb.Tecnico].nome'] - Elisson Silva
Is the table actually named mb.Tecnico and is that the value of TableName in your error message? - Alex K.
The SQL is correctly formed (the square brackets are needed to escape the table name because it contains a .). The error either means you are connected to the wrong database or the table is bound to a schema that you are not specifying. - Alex K.

1 Answers

0
votes

When the names of identifiers do not comply with the rules for regular identifiers, they must be delimited by double quotation marks or brackets.

Under "Rules for Regular Identifiers" on MSDN

  1. Embedded spaces or special characters are not allowed.