Having a special character such as a period in a python key
<p>I'm trying to generate sql insert statements using sqlalchemy like this.</p>

<pre><code>def get_sql(self):
    """Returns SQL as String"""
    baz_ins = baz.insert().values(
        Id=self._id,
        Foo.Bar=self.foo_dot_bar,
    )
    return str(baz_ins.compile(dialect=mysql.dialect(),
                             compile_kwargs={"literal_binds": True}))
</code></pre>

<p>This returns <code>keyword cannot be expression</code>. Escaping the period like <code>\.</code> also doesn't work.</p>

<p>One solution I came up with is using <code>FooDOTBar instead of Foo.Bar</code> and then replacing all "DOT" with "." in the generated sql files, this corrupts some other data though and is not optimal. Any better suggestions to deal with this from the ground up?</p>

<p>In your query, <code>values</code> can be <a href="http://docs.sqlalchemy.org/en/latest/core/dml.html#sqlalchemy.sql.expression.Insert.values" rel="nofollow">assigned with dictionary</a>. So you can do something like:</p>

<pre><code>baz_ins = baz.insert().values({"Id": self._id, "Foo.Bar": self.foo_dot_bar})
</code></pre>

<p>Check out the <a href="http://docs.sqlalchemy.org/en/latest/core/dml.html#sqlalchemy.sql.expression.insert" rel="nofollow">insert</a> documentation for more options</p>

python sqlalchemy