Doubt in the insertion of data realizing the relationship in another table

0

I'm trying to insert an Address into the adresses table and refer to a User already registered without Address in the user table, through the _id that is generated when I enter an Address. Return this _id and insert it into the adresses_id column of the User that has cpf equal to that of the User that will receive this Address.

WITH new_adresses AS (
INSERT INTO adresses (cep, street, neighborhood, city, state, number_house, complement)
        VALUES ('11222333', 'Rua Qualquer', 'Bairro', 'Cidade', 'ES', 777, 'Ed. Edifício')
        RETURNING _id)
        INSERT INTO users (adresses_id)
        VALUES (SELECT _id FROM new_adresses)
        WHERE cpf = '111.222.333-00'

I'm not able to perform the insert this way. The following error message is displayed:

ERROR: syntax error at or near "SELECT"
Posição: 281
    
asked by anonymous 10.10.2018 / 18:26

2 answers

0

I was making an error using INSERT instead of UPDATE SET. The correct query looks like this:

WITH new_adresses AS (
INSERT INTO adresses (cep, street, neighborhood, city, state, number_house, complement)
        VALUES ('11222333', 'Rua do Karalho', 'Fon', 'YoCity', 'YO', 777, 'Ed. YoPredio')
        RETURNING _id)
        UPDATE users
        SET adresses_id = (SELECT _id FROM new_adresses)
 WHERE cpf = '139.127.217-00'
    
10.10.2018 / 18:59
0

Hello, you are closing the parentheses in the wrong place, close it after informing cpf, getting: ... VALUES (SELECT _id FROM new_adresses WHERE cpf = '111.222.333-00')

    
10.10.2018 / 18:47