Skip to content

String slices as arrays are not correctly set as valid value in PostgreSQL using ValuesQuery #1278

@sixstone-qq

Description

@sixstone-qq

Provided this model:

	type Data struct {
		ID   int64    `bun:",pk"`
		Tags []string `bun:",array"`
	}

And using this ValuesQuery to create a CTE does not create a valid SQL for PostgreSQL:

	_, err := db.NewCreateTable().Model((*Data)(nil)).Exec(ctx)

	data := make([]Data, 10)
	for i := range 10 {
		data[i] = Data{
			ID:   int64(i),
			Tags: []string{"foo", "bar"},
		}
	}

	q := tr.db.NewValues(&data)
	t.Log(tr.db.NewSelect().With("foo", &data).String())

This prints:

WITH "foo" ("id", "tags") AS (VALUES (0::BIGINT, '{"foo","bar"}'::VARCHAR[]), (1::BIGINT, '{"foo","bar"}'::VARCHAR[]), (2::BIGINT, '{"foo","bar"}'::VARCHAR[]), (3::BIGINT, '{"foo","bar"}'::VARCHAR[]), (4::BIGINT, '{"foo","bar"}'::VARCHAR[]), (5::BIGINT, '{"foo","bar"}'::VARCHAR[]), (6::BIGINT, '{"foo","bar"}'::VARCHAR[]), (7::BIGINT, '{"foo","bar"}'::VARCHAR[]), (8::BIGINT, '{"foo","bar"}'::VARCHAR[]), (9::BIGINT, '{"foo","bar"}'::VARCHAR[])) SELECT *

which is invalid with:

ERROR: column \"tags\" is of type character varying[] but expression is of type character varying (SQLSTATE 42804

It seems to work for other cases like insert.

Could you help me to make it work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions