django-concurrency icon indicating copy to clipboard operation
django-concurrency copied to clipboard

Trigger already exists

Open boatcoder opened this issue 5 years ago • 2 comments

During migrations (after concurrency has been added I'm running into this:

CREATE OR REPLACE FUNCTION func_concurrency_games_gamecontext_version()
    RETURNS TRIGGER as
    '
    BEGIN
       NEW.version = OLD.version +1;
        RETURN NEW;
    END;
    ' language 'plpgsql';

CREATE TRIGGER concurrency_games_gamecontext_version BEFORE UPDATE
    ON games_gamecontext FOR EACH ROW
    EXECUTE PROCEDURE func_concurrency_games_gamecontext_version();

trigger "concurrency_games_gamecontext_version" for relation "games_gamecontext" already exists

I think there is something missing right before CREATE TRIGGER and that would be DROP TRIGGER IF EXISTS concurrency_games_gamecontext_version

Unfortunately there is no CREATE OR REPLACE TRIGGER so I think if the code is going to create it, it should drop it first IF EXISTS.

I have no CONCURRENCY settings in my settings.py file

boatcoder avatar Nov 21 '20 01:11 boatcoder

Not sure why there is a drop_clause unless it is for when your remove concurrency from the project.

class PostgreSQL(TriggerFactory):
    drop_clause = r"""DROP TRIGGER IF EXISTS {trigger_name} ON {opts.db_table};"""

    update_clause = r"""CREATE OR REPLACE FUNCTION func_{trigger_name}()
    RETURNS TRIGGER as
    '
    BEGIN
       NEW.{field.column} = OLD.{field.column} +1;
        RETURN NEW;
    END;
    ' language 'plpgsql';

DROP TRIGGER IF EXISTS {trigger_name} ON {opts.db_table};
CREATE TRIGGER {trigger_name} BEFORE UPDATE
    ON {opts.db_table} FOR EACH ROW
    EXECUTE PROCEDURE func_{trigger_name}();
    """

Fixes the problem for me in triggers.py

boatcoder avatar Nov 21 '20 02:11 boatcoder

Seems you added triggers manually or using the management command. can you remove them with ./manage.py triggers... ?

saxix avatar Mar 07 '21 12:03 saxix

closing due to missing feedback

saxix avatar Jan 27 '24 17:01 saxix