#989·sqlx

Would it be possible to expand a slice of structs inside a struct within the same sql statement?

Author: kkrimeCreated May 29, 2026Updated Aug 27, 2026

For example;

type Order struct {
	id    int     `db:"id"`
	Items []*Item `db:"items"`
}

type Item struct {
	name     string `db:"name"`
	Quantity int32  `db:"quantity"`
}

If I wanted to use use all the above in one statement like;

WITH order_ AS (
	INSERT INTO orders (total_price) VALUES (:id) RETURNING id
),
item_ AS (
	INSERT INTO order_items_map (order_id, name, quantity ) VALUES ((SELECT id FROM order_), :items.name, :items.quantity)
)

SELECT id as order_number from order_;

Currently the above does not work, the second insert does not expand the items, but it does work if I was to use just the second insert statement and pass in an array of items;

NamedQueryContext(`INSERT INTO order_item_map (order_id, item_id, quantity ) VALUES ((SELECT id FROM order_) :name, :quantity)`, []*Items{...})

my question is, is there a reason why this couldn't be implemented in sqlx?