Search code examples
cassandracqluser-defined-typesscylla

How to insert values in a table which has user defined types?


I am working with Scylla (Cassandra) db and trying to create tables which is dealing with User-Defined Types as shown below:

CREATE TYPE process (
    id int,
    discount float
);

CREATE TYPE service (
    id int,
    url text
);

CREATE TABLE data (
    id int PRIMARY KEY,
    fname text,
    lname text,
    service set<frozen<service>>,
    monthly_process frozen<process>
);

My confusion is how can I insert data in my data table. Problem is I am confuse how process and service type works here and how can I insert values in them?

I tried with below example but it gave me an error:

insert into test (id, fname, lname, service, monthly_process ) 
values (1, 'abc', 'world', {'service': [{'id':1, 'url': 'some_url1'},
{'id':2, 'url': 'some_url2'}]}, {'id':1, 'discount': 10.0});

Error I got:

InvalidRequest: Error from server: code=2200 [Invalid query]
message="Invalid map literal for service of type set<frozen<service>>"

Solution

  • Here is the working version of your query;

    insert into data (id, fname, lname, service, monthly_process)
    values (2, 'abc', 'world', {{id: 1, url: 'some'}, {id : 2, url:'another'}}, {id:1, discount: 10.0});
    
    • service set<frozen<service>> format would be {{id:1, url:'a'}, {id:2, url:'b'}}
    • monthly_process frozen<process> format is {id:1, discount: 10.0}