Skip to content

im.OnConflict restriction of using code generated structs #581

@atzedus

Description

@atzedus

For example, if I need to construct an ON CONFLICT query using generated structs, I expect to do something like:

safeCols := models.Tokens.Columns
rawCols := dbinfo.Tokens.Columns

createdToken, err := dbmodels.Tokens.Insert(
    tokenSetter,
    im.OnConflict(safeCols.Account.String()).
        DoUpdate(im.SetExcluded(
            models.Tokens.Columns.Except(rawCols.ID.Name).Names()...,
        )),
).One(ctx, r.db)

However, this is not valid because the query builder constructs something like INSERT ..... ON CONFLICT "tokens"."account" ..., while PostgreSQL expects INSERT ..... ON CONFLICT "account" ....

Using im.OnConflict(safeCols.Account) also doesn't work because of the Express method, which also generates INSERT ..... ON CONFLICT "tokens"."account" ....

The only way I can make this work is to call it like:

im.OnConflict(psql.Quote(dbinfo.Tokens.Columns.Account.Name))

But this forces me to remember to call psql.Quote, which is a potential source of errors if I miss it.

Perhaps it would be possible to generate a Name() helper for Columns, similar to the one for the whole table? Then it would be possible to write something like:

im.OnConflict(models.Tokens.Columns.Account.Name())

This would not force me to use the dbinfo package, which is potentially dangerous.

Alternatively, maybe OnConflict could detect if it receives a psql.Expression value and remove the "table" part when calling WriteSQL.

All of this also applies to the models.Tokens.Columns.Except() helper, which can only receive string types and forces me to use the dbinfo package to provide the cols parameter for it.

Maybe I'm using the wrong approach and there's a proper way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions