#34955: Make available the string concatenation operator `||`  for PostgreSQL
-------------------------------------+-------------------------------------
     Reporter:  Paolo Melchiorre     |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  field, database,     |             Triage Stage:
  generated, output_field            |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Paolo Melchiorre):

 Replying to [comment:8 Mariusz Felisiak]:
 > If casting to text data-type fixes the issue then I'd consider
 documenting this workaround instead of adding a PostgreSQL-specific
 function (where text casting would still be required). For example, adding
 the following sentence:
 >
 > ''"In order to make an `IMMUTABLE` expression  `MUTABLE` on PostgreSQL,
 you can wrap the expression with `Cast()`, e.g.
 `Cast(Concat(F("first_name"), Value(" "), F("last_name")),
 TextField())`."''
 >
 > in the warning about `IMMUTABLE` functions on PostgreSQL.


 Unfortunately the SQL code generate by the Django ORM is still broken in
 PostgreSQL.

 I wrote the class  as susggested by Mariusz:
 {{{
 #!python
 class Person(models.Model):
     first_name = models.CharField()
     last_name = models.CharField()
     full_name = models.GeneratedField(
         expression=Cast(
             Concat("first_name", V(" "), "last_name"),
             models.TextField(),
         ),
         db_persist=True,
         output_field=models.TextField(),
     )
 }}}

 But the migrations generate this SQL code:
 {{{
 #!sql
 CREATE TABLE "samples_person" (
     "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
     "first_name" varchar NOT NULL,
     "last_name" varchar NOT NULL,
     "full_name" text GENERATED ALWAYS AS (
         (
             CONCAT(
                 ("first_name")::text,
                 (CONCAT((' ')::text, ("last_name")::text))::text
             )
         )::text
     ) STORED
 );
 }}}

 that generated this error message `psycopg.errors.InvalidObjectDefinition:
 generation expression is not immutable`

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34955#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018bb3cf19c9-4fddb1a8-9768-4597-a959-dbe5da511a2c-000000%40eu-central-1.amazonses.com.

Reply via email to