Django

Code

Ticket #121 (closed: fixed)

Opened 3 years ago

Last modified 1 year ago

[patch] Names in SQL should be quoted

Reported by: sune.kirkeby@gmail.com Assigned to: adrian
Milestone: Component: Metasystem
Version: Keywords: sql
Cc: rmunn@pobox.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Some valid Python-identifiers are reserved words in SQL-databases, for instance "when" in PostgreSQL. It would be very nice, if Django would quote all names (e.g. schema, table, row) in SQL statements, so these database-reserved words would not pose problems.

Attachments

quote_name.patch (2.4 kB) - added by rmunn@pobox.com on 07/28/05 00:13:06.
Patch to add quote_name() to all db backends
change_all_sql.patch (36.3 kB) - added by rmunn@pobox.com on 07/28/05 00:20:18.
Patch to quote all SQL identifiers with db.quote_name()
fixed_quote_name.patch (2.4 kB) - added by rmunn@pobox.com on 07/28/05 02:45:48.
Fixed patch to add quote_name() to all db backends - previous patch had wrong linecounts

Change History

07/27/05 18:39:18 changed by rmunn@pobox.com

In both PostgreSQL and SQLite, identifiers such as table and field names are quoted using the " (double-quote) character. In MySQL, identifiers are quoted using the ` (backtick) character. A helper function will be needed to quote names appropriately for each database engine.

07/27/05 18:41:21 changed by rmunn@pobox.com

The above patch adds the necessary helper function. Now it's "just" a case of going through the Django source and finding all the places where field names need to be quoted. Not exactly non-trivial, but doable.

07/27/05 18:41:36 changed by rmunn@pobox.com

I meant "Not exactly trivial", of course.

07/28/05 00:12:33 changed by rmunn@pobox.com

After working on quoting the field names for a short while, I discovered that quote_identifier() is far too long to type, especially when in some places it needs to be repeated four times for a single SQL statement! So I renamed it to quote_name(). I also added a little extra "smarts" to the function, so that if an identifier has already been quoted it won't get quoted again. Please ignore the quote_identifier.patch above and replace it with the following quote_name.patch:

07/28/05 00:13:06 changed by rmunn@pobox.com

  • attachment quote_name.patch added.

Patch to add quote_name() to all db backends

07/28/05 00:18:51 changed by rmunn@pobox.com

And finally, here's the (very large) patch to correctly quote all identifiers (table and field names) in the SQL of all of Django. Phew!

07/28/05 00:20:18 changed by rmunn@pobox.com

  • attachment change_all_sql.patch added.

Patch to quote all SQL identifiers with db.quote_name()

07/28/05 02:44:44 changed by rmunn@pobox.com

  • summary changed from Names in SQL should be quoted to [patch] Names in SQL should be quoted.

Tagging summary with [patch] to flag it for developers' attention. Also noticed that I uploaded a broken version of the quote_name.patch; fixed version of that patch coming up.

07/28/05 02:45:48 changed by rmunn@pobox.com

  • attachment fixed_quote_name.patch added.

Fixed patch to add quote_name() to all db backends - previous patch had wrong linecounts

07/28/05 03:57:58 changed by anonymous

07/28/05 09:04:15 changed by rmunn@pobox.com

  • cc set to rmunn@pobox.com.

07/31/05 19:22:55 changed by rmunn@pobox.com

It's worth noting Ian Sparks' comment at http://www.djangoproject.com/weblog/2005/jul/21/sqlite3/#c250 that if we quote all field names in the SQL, then those field names become case-sensitive. This shouldn't have any effect on the generated SQL, since we always use code like [f.name for f in self._meta.fields] and so the names being sent to the database are already case-consistent. But if we end up with strange errors immediately on applying this patch, one troubleshooting approach will be to look for any case-inconsistencies in field names throughout the Django code.that's one possible place to look

09/01/05 18:11:21 changed by adrian

  • component changed from Core framework to Metasystem.

09/14/05 12:43:16 changed by adrian

  • status changed from new to assigned.

10/29/05 04:36:42 changed by hugo

  • milestone set to Version 1.0.

I think this really should go in, especially in the light of more db backends coming up. The problem is, more backends means more reserved words, means people get short on column names - or problems with incompatibilities over different databases are bound to happen, as people tend to use short names that might be reserved by some database.

I set the milestone to 1.0 because I think this really should go in, as it doesn't change djangos functionality at all, but makes portability of applications much simpler.

10/31/05 19:32:56 changed by adrian

(In [1039]) Added quote_name hook for each database backend. Refs #121. Thanks, Robin Munn -- we miss you.

11/12/05 03:07:41 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

I have a recent version of the quote-names patch which works like a charm here. Any interest in merging this soon? I don't feel like spamming the list of attached files with an updated patch for every change in d.c.meta which touches the SQL :)

11/12/05 23:13:04 changed by adrian

Sure, go ahead and attach the patch to this ticket. I'm planning on integrating this shortly -- hopefully on Sunday, if I have the time.

11/13/05 19:44:37 changed by adrian

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [1224]) Fixed #121 -- Django now quotes all names in SQL queries. Also added unit tests to confirm. Thanks, Robin Munn and Sune.

01/17/07 16:12:17 changed by

  • milestone deleted.

Milestone Version 1.0 deleted


Add/Change #121 ([patch] Names in SQL should be quoted)




Change Properties
Action