-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
count系、updatedAt系のカラムはトリガーで更新するようにしたい #8442
Comments
Translation for those who find this :) If you do it on the application side, you can not deal with CASCADE deletion and it is useless in terms of processing |
しょうがないから生SQL書くかしら |
こんな感じか CREATE FUNCTION user_update_notes_count() RETURNS trigger AS $body$
BEGIN
IF (TG_OP = 'INSERT') THEN
UPDATE "user" SET "notesCount" = "notesCount" + 1 WHERE "user"."id" = NEW."userId";
RETURN NEW;
ELSE
UPDATE "user" SET "notesCount" = "notesCount" - 1 WHERE "user"."id" = OLD."userId";
RETURN OLD;
END IF;
END;
$body$
LANGUAGE plpgsql; CREATE TRIGGER user_update_notes_count_trigger AFTER INSERT OR DELETE ON "note" FOR EACH ROW
EXECUTE FUNCTION user_update_notes_count(); ローカルで動作確認 |
それだと結局CASCADE削除とかに対応できてなくない? |
もちろんdelete分も作る |
あーCASCADEでdeleteされるレコードはトリガ発火しないとかそういうこと? |
すみませんが |
PR募集しています |
そもそもキューイングしてバルク的に更新した方が良さそう |
Summary
アプリケーションサイドでやるとCASCADE削除とかに対応できないし処理的にも無駄なため
ただTypeORMでは現在migrationに直接SQL書く以外でトリガーの定義はサポートされていない
typeorm/typeorm#1082
The text was updated successfully, but these errors were encountered: