From dd65d8d725996b428311ddb79f41eae66969bade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Mathieu?= Date: Fri, 21 Apr 2023 08:44:58 +0200 Subject: [PATCH 1/2] Allow extra field when inserting with prepared queries Let's say you have a message: ``` class Address(object): def __init__(self, street, zipcode, **kwargs): self.street = street self.zipcode = zipcode cluster.register_user_type('mykeyspace', 'address', Address) ``` And let's say the type actually contains another field, let's call i `raw_address` Then inserting data through a prepared statement will actually fail : the driver will complain `raw_address` is missing. This change addresses that, as any field should be optional. --- cassandra/cqltypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cassandra/cqltypes.py b/cassandra/cqltypes.py index 7946a63af8..88a2b5fd4b 100644 --- a/cassandra/cqltypes.py +++ b/cassandra/cqltypes.py @@ -1026,7 +1026,7 @@ def serialize_safe(cls, val, protocol_version): try: item = val[i] except TypeError: - item = getattr(val, fieldname) + item = getattr(val, fieldname, None) if item is not None: packed_item = subtype.to_binary(item, proto_version) From 1740cfb025d214dd0094b4824064a8fa9953ce29 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Wed, 26 Apr 2023 14:03:04 +0200 Subject: [PATCH 2/2] feat: wip --- cassandra/cqltypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cassandra/cqltypes.py b/cassandra/cqltypes.py index 88a2b5fd4b..8167b3b894 100644 --- a/cassandra/cqltypes.py +++ b/cassandra/cqltypes.py @@ -1027,6 +1027,8 @@ def serialize_safe(cls, val, protocol_version): item = val[i] except TypeError: item = getattr(val, fieldname, None) + if item is None and not hasattr(val, fieldname): + log.warning(f"field {fieldname} is part of the UDT {cls.typename} but is not present in the value {val}") if item is not None: packed_item = subtype.to_binary(item, proto_version)