Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks triggered by upsert #236

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Hooks triggered by upsert #236

wants to merge 18 commits into from

Conversation

Rochet2
Copy link

@Rochet2 Rochet2 commented Sep 8, 2022

When a Model.upsert is made, only beforeUpsert, afterUpsert, beforeValidate, afterValidate hooks are triggered.
No other hooks trigger, such as update or insert/create hooks or beforeSave, afterSave.

In https://sequelize.org/docs/v6/other-topics/upgrade/#modelupsert and https://sequelize.org/api/v6/class/src/model.js~model#static-method-upsert
it is said that upsert returns the new instance and created variable indicating if insert or update was made. However, For SQLite/Postgres, created value will always be null.

Output:

2022-09-08T22:10:25.8429561Z --------------------------------------------------------------------
2022-09-08T22:10:25.8435758Z ===== Running SSCCE for POSTGRES-NATIVE with Sequelize v6.21.5 =====
2022-09-08T22:10:25.8436683Z --------------------------------------------------------------------
2022-09-08T22:10:25.8437166Z 
2022-09-08T22:10:26.9250098Z [Sequelize] Executed (default): DROP TABLE IF EXISTS "Foos" CASCADE; [Elapsed time: 0 ms] 
2022-09-08T22:10:26.9250375Z 
2022-09-08T22:10:26.9346450Z [Sequelize] Executed (default): SELECT DISTINCT tc.constraint_name as constraint_name, tc.constraint_schema as constraint_schema, tc.constraint_catalog as constraint_catalog, tc.table_name as table_name,tc.table_schema as table_schema,tc.table_catalog as table_catalog,tc.initially_deferred as initially_deferred,tc.is_deferrable as is_deferrable,kcu.column_name as column_name,ccu.table_schema  AS referenced_table_schema,ccu.table_catalog  AS referenced_table_catalog,ccu.table_name  AS referenced_table_name,ccu.column_name AS referenced_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = 'Foos' AND tc.table_catalog = 'sequelize_test' [Elapsed time: 8 ms] 
2022-09-08T22:10:26.9353620Z 
2022-09-08T22:10:26.9353985Z [Sequelize] Executed (default): DROP TABLE IF EXISTS "Foos" CASCADE; [Elapsed time: 0 ms] 
2022-09-08T22:10:26.9354215Z 
2022-09-08T22:10:26.9356533Z beforeSync
2022-09-08T22:10:26.9362125Z [Sequelize] Executed (default): DROP TABLE IF EXISTS "Foos" CASCADE; [Elapsed time: 1 ms] 
2022-09-08T22:10:26.9362539Z 
2022-09-08T22:10:26.9444100Z [Sequelize] Executed (default): CREATE TABLE IF NOT EXISTS "Foos" ("id"  SERIAL , "name" TEXT, PRIMARY KEY ("id")); [Elapsed time: 7 ms] 
2022-09-08T22:10:26.9444623Z 
2022-09-08T22:10:26.9470590Z [Sequelize] Executed (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'Foos' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname; [Elapsed time: 2 ms] 
2022-09-08T22:10:26.9471556Z 
2022-09-08T22:10:26.9489793Z afterSync
2022-09-08T22:10:26.9503328Z Upserting action: insert
2022-09-08T22:10:26.9517844Z beforeValidate
2022-09-08T22:10:26.9523034Z afterValidate
2022-09-08T22:10:26.9524871Z beforeUpsert
2022-09-08T22:10:26.9562816Z [Sequelize] Executed (default): INSERT INTO "Foos" ("id","name") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "id"=EXCLUDED."id","name"=EXCLUDED."name" RETURNING "id","name"; 1, "bar" [Elapsed time: 2 ms] 
2022-09-08T22:10:26.9563328Z 
2022-09-08T22:10:26.9567073Z afterUpsert
2022-09-08T22:10:26.9585761Z [ Foo {
2022-09-08T22:10:26.9586317Z     dataValues: { id: 1, name: 'bar' },
2022-09-08T22:10:26.9586819Z     _previousDataValues: { id: undefined, name: undefined },
2022-09-08T22:10:26.9634305Z     uniqno: 1,
2022-09-08T22:10:26.9634933Z     _changed: Set { 'id', 'name' },
2022-09-08T22:10:26.9635401Z     _options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
2022-09-08T22:10:26.9635700Z     isNewRecord: false },
2022-09-08T22:10:26.9635900Z   null ]
2022-09-08T22:10:26.9636147Z Upserting action: update
2022-09-08T22:10:26.9636388Z beforeValidate
2022-09-08T22:10:26.9636586Z afterValidate
2022-09-08T22:10:26.9636797Z beforeUpsert
2022-09-08T22:10:26.9637476Z [Sequelize] Executed (default): INSERT INTO "Foos" ("id","name") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "id"=EXCLUDED."id","name"=EXCLUDED."name" RETURNING "id","name"; 1, "baz" [Elapsed time: 0 ms] 
2022-09-08T22:10:26.9637911Z 
2022-09-08T22:10:26.9638001Z afterUpsert
2022-09-08T22:10:26.9638202Z [ Foo {
2022-09-08T22:10:26.9638505Z     dataValues: { id: 1, name: 'baz' },
2022-09-08T22:10:26.9638843Z     _previousDataValues: { id: undefined, name: undefined },
2022-09-08T22:10:26.9639107Z     uniqno: 1,
2022-09-08T22:10:26.9639395Z     _changed: Set { 'id', 'name' },
2022-09-08T22:10:26.9639827Z     _options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
2022-09-08T22:10:26.9640121Z     isNewRecord: false },
2022-09-08T22:10:26.9640319Z   null ]
2022-09-08T22:10:26.9640506Z End

@Rochet2 Rochet2 changed the title Patch 2 Hooks triggered by upsert Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant