uptrace / bun

SQL-first Golang ORM

Home Page:https://bun.uptrace.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Postgres update join not working

edstef opened this issue · comments

I see in #908 that there is a feature for update query with join.
I updated to github.com/uptrace/bun v1.1.17 and I get no golang errors but do get errors from postgres:

pgdriver.Error{m:map[uint8]string{0x43:"42601", 0x46:"scan.l", 0x4c:"1192", 0x4d:"syntax error at or near \"INNER\"", 0x50:"72", 0x52:"scanner_yyerror"

My code:

_, err := m.db.NewUpdate().
    Model(&PlayerGameRelation{}).
    Set("state = ?", PLAYER_GAME_RELATION_STATES.QUIT).
    Join("INNER JOIN games ON player_game_relations.game = game.id").
    Where("player_game_relations.player = ? AND game.state = ?", playerId, gameState).
    Exec(context.Background())

In postgres it says to update join using FROM clause but bun doesn't support that on update query. From postgres logs the full statement is:

UPDATE "player_game_relations" AS "player_game_relation" SET state = 1 INNER JOIN game ON player_game_relations.game = game.id WHERE (player_game_relations.player = '7319998d-0223-4929-997f-ea667f587695' AND game.state = 0)```

@edstef

It seems to work as expected if you change the query to be passed to the Join method from the Join clause to the From clause as shown below(I can't guarantee that it will work correctly since I haven't actually tested it).

    Join("INNER JOIN games ON player_game_relations.game = game.id")
    ↓
    Join("From games ON player_game_relations.game = game.id")