I am developing an application with Python3 and Apache Cassandra database for my backend but using with UDT's in Cassandra, I have a problem, The code is below so I did try everything and tried to patch things up, however it didn't do well but said KeyError: 0
located in dbsession.execute(insert_stat, insert_var)
. So I print()
out query
, insert_stat
and insert_var
but I don't see any problem or didn't mention in the logs how it causes a KeyError.
Python Code:
query = "INSERT INTO accounts (id, displayname, username, email, joined, status, password, bio, pinnedupdate, interest, notice, details, connection, purge) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
insert_stat = dbsession.prepare(query)
insert_var = (
userid,
limit_string_length(req['username'], 20),
limit_string_length(username_string(req['username']), 20),
[req['email']],
int(datetime.now().timestamp())*1000,
'public',
str(bcrypt.hashpw(password_bytes, bcrypt.gensalt(12)).decode('utf-8')),
'',
None,
{
'fields': [],
'tags': [],
'topics': []
},
{
'acknowledged': True,
'code': 0,
'customproflock': False,
'dmlock': False,
'suspendreason': '',
'suspended': False,
'updatephotolock': False,
'updatelock': False,
'updatevideolock': False
},
{
'emailapproved': False,
'labels': [],
'likesprivate': False,
'private': False,
'pronouns': '',
'verified': False,
'website': ''
},
[],
{
'status': 'public',
'reason': '',
'schedule': int(datetime.now().timestamp())*1000
}
)
dbsession.execute(insert_stat, insert_var)
Cassandra Structure:
CREATE TYPE account_interest (
topics list<text>,
fields list<text>,
tags list<text>,
);
CREATE TYPE account_notice (
code int,
updatelock boolean,
updatephotolock boolean,
updatevideolock boolean,
customproflock boolean,
dmlock boolean,
suspended boolean,
suspendreason text,
acknowledged boolean,
);
CREATE TYPE account_detail (
verified boolean,
private boolean,
website text,
emailapproved boolean,
pronouns text,
labels list<text>,
likesprivate boolean,
);
CREATE TYPE connection (
application text,
tokens list<text>,
);
CREATE TYPE purge (
status text,
reason text,
schedule timestamp,
);
CREATE TABLE accounts (
id uuid,
username varchar,
displayname text,
email set<text>,
status varchar,
joined timestamp,
bio text,
password varchar,
pinnedupdate uuid,
interest FROZEN<account_interest>,
notice FROZEN<account_notice>,
details FROZEN<account_detail>,
connection set<FROZEN<connection>>,
purge FROZEN<purge>,
PRIMARY KEY (id)
);
This was answered previously by Erick Ramirez I believe, and the answer was to use a simple class with the same structure as your UDT, you can read that here: Batch insert fails for table with a CQL map and nested UDT If that doesn't get you going, please reach out.