Opened 6 years ago

Last modified 6 years ago

#29672 closed Bug

Returns an empty model field that is filled with a trigger in the database. — at Version 1

Reported by: Vaskevich Aleksander Owned by: nobody
Component: Uncategorized Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vaskevich Aleksander)

my model

blank_and_null = {'blank': True, 'null': True}

class Message(....):
    ....
    chat_local_id = models.BigIntegerField(
        verbose_name='local id message for chat',
        **blank_and_null)
    ....

I transferred some logic to the trigger database.
Here is my trigger

CREATE OR REPLACE FUNCTION public.inc_chat_local_id()
     RETURNS trigger
     LANGUAGE 'plpgsql' AS
$BODY$
DECLARE
	new_count_message INTEGER;
BEGIN
   	UPDATE v1_chat_chat SET count_message = count_message + 1
 		WHERE id = NEW.chat_id;

	SELECT v1_chat_chat.count_message INTO new_count_message FROM v1_chat_chat
		WHERE id = NEW.chat_id;

	NEW.chat_local_id = new_count_message;

 	RETURN NEW;
END;
$BODY$;

DROP TRIGGER IF EXISTS generate_chat_local_id on v1_chat_message;

CREATE TRIGGER generate_chat_local_id
  BEFORE INSERT
  ON v1_chat_message
  FOR EACH ROW
  EXECUTE PROCEDURE inc_chat_local_id();

It works as I expect
before insertion requests, it sets the value of chat_local_id
when I execute the insert query through django
chat_local_id empty field
when I re-query this entry, the chat_local_id is set to

my Python version: 3.6
my database: Postgres

Change History (1)

comment:1 by Vaskevich Aleksander, 6 years ago

Description: modified (diff)
Type: UncategorizedBug
Note: See TracTickets for help on using tickets.
Back to Top