Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Show examples of how to format objects names in SQL #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions website/Contribution/CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,24 @@ The second reason, which is almost as important, is that it is the job of the da
'WHERE parent_id IN %s',
(tuple(ids),))

In some cases you could need to create queries where the table name or some other elements are required to be inserted dinamically, in that cases use the ``AsIs`` class like the examples to avoid sql errors for bad formatting:

.. code-block:: python

# Bad, SQL syntax error is raised because of a bad formatting
# Example 1
cr.execute('SELECT * FROM %s WHERE id=%s', (table, 1))
# Example 2
cr.execute('DROP SEQUENCE IF EXISTS ir_sequence_%s', (number,))

# Good
from psycopg2.extensions import AsIs
...
# Example 1
cr.execute('SELECT * FROM %s WHERE id=%s', (AsIs(table), 1))
# Example 2
cr.execute('DROP SEQUENCE IF EXISTS ir_sequence_%s', (AsIs(number),))

This is very important, so please be careful also when refactoring, and most importantly do not copy these patterns!

Here is a [memorable example](http://www.bobby-tables.com) to help you remember what the issue is about (but do not copy the code there).
Expand Down