Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem
- PostgreSQL converts all table column names into lowercase, unless quoted.
Goal
- Retain letter-casing of table column names.
Details
- PostgreSQL treats all identifiers in schema operations as lowercase, so the following creates NOT the table you intended:
CREATE TABLE Events ( EventId SERIAL NOT NULL PRIMARY KEY, );
- To retain natural casing in schema operations, identifiers have to be quoted:
CREATE TABLE "Events" ( "EventId" SERIAL NOT NULL PRIMARY KEY, );
Proposed solution
- Quote the identifiers to retain their natural casing.
Links
- http://binodsblog.blogspot.de/2011/02/postgresql-is-case-sensitive.html
- http://www.postgresql.org/docs/8.0/static/sql-syntax.html#AEN1148
- http://codesnippets.joyent.com/posts/show/1701
Related issues
Comment | File | Size | Author |
---|---|---|---|
drupal8.postgres-case.0.patch | 729 bytes | sun | |
Comments
Comment #2
burningdog CreditAttribution: burningdog commenteddrupal8.postgres-case.0.patch queued for re-testing.
Comment #3
burningdog CreditAttribution: burningdog commentedGreat! Patch passes! This will fix #1529268: 2.x series is incompatible with PostgreSQL due to use of uppercase in column names which is how I found my way here. Wrapping the identifiers in quotes maintains the the letter-casing of column names, which is exactly what we want (otherwise postgres queries die a painful PDO Exception death).
Does this need to be committed to 8.x before backporting to 7.x?
Comment #4
burningdog CreditAttribution: burningdog commentedWhat this patch misses is the ability to wrap with quotes each field which contains upper case. Example:
contentId
needs to be wrapped in quotes.In the following example,
t.contentId
needs to bet."contentId"
In drupal 7, part of the solution is to implement the escapeField() function from the DatabaseConnection class, which works as follows:
But there still needs to be something done on module installation, so that field comments can be added. For instance, with the previous code implemented (in d7), I get the following error upon installation of the Mollom module:
As you can see, contentId is not wrapped in quotes (and I don't know enough about the db abstraction layer to know where that query gets built).
Sorry for the d7 example - I haven't started working with d8 yet. I thought it was useful to post here because the same problem needs to be solved in d8.
Aside: wouldn't it be simpler to to run
strtolower();
on all fields, in the pgsql implementation, rather than wrapping all fields with quotes to preserve their uppercase-ness?Comment #5
burningdog CreditAttribution: burningdog commentedHere's the d7 correction in createTableSql() in includes/database/pgsql/schema.inc:
The extra bit is the check:
Then Mollom installs correctly.
Comment #6
drupdan3 CreditAttribution: drupdan3 commentedWith both the patch in #5 and the initial drupal8.postgres-case.0.patch at the beginning of this thread, Mollom installs correctly; however, in operation I get
PDOException: SQLSTATE[42703]: Undefined column: 7 ERROR: column "contentid" of relation "mollom" does not exist LINE 1: INSERT INTO mollom (entity, id, contentId, captchaId... INSERT INTO mollom (entity, id, contentId, captchaId, form_id, changed, moderate, spamScore, spamClassification, solved, qualityScore, profanityScore, reason, languages) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( ) in drupal_write_record() (line 7166 of /var/www/site.com/news/includes/common.inc).
because again PostgreSQL lower-cases unquoted field names, which yields to a mismatch with the schema.
Comment #7
bzrudi71 CreditAttribution: bzrudi71 commentedSeems this is more or less a duplicate of #1600670: Cannot query Postgres database that has column names with capital letters.
Comment #8
sunIndeed, merged the issue summary into that issue.