How to create many things in one hook only? Why this code doesn't work? #380
Answered
by
vmihailenco
frederikhors
asked this question in
Q&A
-
I would like to know how to create many thing for a table in one hook. Example code that doesn't work: var _ bun.AfterCreateTableHook = (*Player)(nil)
func (*Player) AfterCreateTable(_ context.Context, query *bun.CreateTableQuery) error {
query.ForeignKey(`("team_id") REFERENCES "teams" ("id")`)
_, err := query.DB().Exec(`CREATE TRIGGER someTrigger BEFORE UPDATE ON "players" FOR EACH ROW EXECUTE PROCEDURE someTrigger()`)
if err != nil {
return err
}
return nil
} This doesn't create the ForeignKey, why? If I remove the |
Beta Was this translation helpful? Give feedback.
Answered by
vmihailenco
Dec 26, 2021
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
frederikhors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AfterCreateTable
is run after the table is created soquery.ForeignKey(
("team_id") REFERENCES "teams" ("id"))
does not make any difference / is ignored. You should move it toBeforeCreateTable
.