Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of Insert and Upsert in KDB

I have following table:

           q) t:([s:`symbol$()] id:();id2:`int$())

where 's' is a primary key and 'id' col has general type. I am trying to understand following behavior when inserting a list (string in this ex.) in 'id' column:

a) Upsert works but Insert fails

          q) `t insert (`a;"gg";4)     // 'type 
          q) `t upsert (`a;"gg";4)     // works

b) Insert requires primary key to be enlisted as well:

    q)`t insert  (`a;enlist "gg";4)    // 'length

    q)`t insert  (enlist `a;enlist "gg";4)  // works

What's going on behind the scene?

like image 894
Rahul Avatar asked Aug 07 '15 13:08

Rahul


Video Answer


1 Answers

I believe the problem is with "gg" - it is a list so the insert gets confused whether you're trying to insert one record or multiple. This:

`t insert (`a;"g";4)

works just fine. Unfortunately I do not know other workaround but give insert a list of records of length one:

`t insert (enlist `c;enlist "gg";enlist 4)

I am not sure what's up with upsert, but it might have something to do with its implementation via amend: .[;();,;]

like image 129
Alexander Balabin Avatar answered Oct 06 '22 20:10

Alexander Balabin