Search code examples
sqlmysqlreserved-wordssqlobject

Using illegal names in MySQL through SQLObject


How to use illegal names for MySQL with SQLObject?

In pure SQL it is possible to use backquotes, say:

    SELECT `select from` FROM table1 WHERE 1;

...can be used to select the field called select from. Is it possible to tell SQLObject to utilize backquotes?


Solution

  • CREATE TABLE table1 (
      id INT(11),
      `select from` VARCHAR(255),
      PRIMARY KEY (id)
    );
    INSERT INTO table1 VALUES(1, 'test value');
    

    to access select from from SQLObject, declare the column with backticks:

    >>> class Table1(SQLObject):
    ...     myIllegallyNamedColumn = Col(dbName="`select from`")
    ... 
    >>> list(Table1.select())
    [<Table1 0 myIllegallyNamedColumn='test value'>]