0

I have a table item with attributes no(integer) and price(integer), also another table cart with attributes no(integer) and items(array of item).

I have some records in items.

When i tried :

INSERT INTO myschema.cart VALUES(1,'{SELECT item from myschema.item}')

I'am getting error malformed record literal.

I expected this to insert all items from myschema.item into the cart record.

1
  • ' denotes a string literal. Commented Jun 6, 2017 at 6:32

2 Answers 2

1

It's hard to give you exact statement without the table structures and such, but you can select into an array:

INSERT INTO myschema.cart (id, item_ids)
SELECT 1, array(SELECT id from myschema.item)

This will select the id's from the item table into an array.

You can test it out by writing:

select array(SELECT id from myschema.item)

Sign up to request clarification or add additional context in comments.

Comments

0

You can't write a subquery inside a string like that.

What you need to do is aggregate the items into a array with array_agg

INSERT INTO myschema.cart
VALUES (1, (SELECT array_agg(item) FROM myschema.item));

Or

INSERT INTO myschema.cart
SELECT 1, array_agg(item) FROM myschema.item;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.