sqlalchemy syntax for using mysql constants
<p>So i am trying to call the mysql function <strong>TIMEDATEDIFF</strong> with the <strong>SECOND</strong> constant as the first parameter like so</p>

<pre><code>    query = session.query(func.TIME(Log.IncidentDetection).label('detection'), func.TIMESTAMPDIFF(SECOND,Log.IncidentDetection, Log.IncidentClear).label("duration")).all()
    print(query)
</code></pre>

<p>I have tried it as a string and I get a mysql/mariadb error:</p>

<pre><code>query = session.query(func.TIME(Log.IncidentDetection).label('detection'), func.TIMESTAMPDIFF("SECOND",Log.IncidentDetection, Log.IncidentClear).label("duration")).all()
print(query)
</code></pre>

<p><strong>Gives me this</strong></p>

<pre><code>sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''SECOND', log.`IncidentDetection`, log.`IncidentClear`) AS duration 
FROM log' at line 1 [SQL: 'SELECT TIME(log.`IncidentDetection`) AS detection, TIMESTAMPDIFF(%(TIMESTAMPDIFF_1)s, log.`IncidentDetection`, log.`IncidentClear`) AS duration \nFROM log'] [parameters: {'TIMESTAMPDIFF_1': 'SECOND'}]
</code></pre>

<p>I am sure it is something simple, some sort of escape sequence or import that I am missing. I have looked through the sqlalchemy documentation to no avail.</p>

<p>To get sqlalchemy to parse the string exactly into the query I used the <code>_literal_as_text()</code> function</p>

<p><strong>Working solution</strong></p>

<hr>

<pre><code>from sqlalchemy.sql.expression import func, _literal_as_text
# ...
query = session.query(func.TIME(Log.IncidentDetection).label('detection'), func.TIMESTAMPDIFF(_literal_as_text("SECOND"),Log.IncidentDetection, Log.IncidentClear).label("duration")).all()
print(query)
</code></pre>

python mysql sqlalchemy literals func