using the ids returned from insert into, for record insertion with foreign key

后端 未结 1 453
滥情空心
滥情空心 2021-01-22 09:06

I have a table

monster(id serial, name varchar, primary key(id))

and i have another table

ranged_monster(id_monster integer, distanc

相关标签:
1条回答
  • 2021-01-22 09:24
    with s(name, distance) as (
        values ('archer goblin', 10), ('dragon', 50)
    ), the_ids as (
        insert into monster(name) 
        select name
        from s
        returning id, name
    )
    insert into ranged_monster (id_monster, distance)
    select id, distance
    from
        s
        inner join
        the_ids using (name)
    
    0 讨论(0)
提交回复
热议问题