python-driver icon indicating copy to clipboard operation
python-driver copied to clipboard

User Defined Type (UDT) no longer recognized when imported and used in a different file than where it was registered

Open nigel5 opened this issue 1 year ago • 3 comments

Problem

Hi, I have a question about the usage of UDTs. I was getting <Error from server: code=2000 [Syntax error in CQL query] message="line 1:278 no viable alternative at input despite registering the UDT with the default connection. After a sometime debugging I found that I needed to register the UDT each time it was imported.

main.py sets up the database cluster and session and then it registers the UDTs. myutilfuncs.py tries to use the UDT with one of the Models but it gets a syntax error. After registering the UDT again then it worked.

I thought once the UDTs are registered with the default connection with is a singleton, then they can be used anywhere in the project since the Models will use that connection.

Is this the expected behaviour or am I missing something to configure? Thanks!

Python version 3.9.6 scylla-driver version 3.26.7

main.py

import mymodels  # Defines a UDT and a table that uses the UDT
import myutilfuncs
from cassandra.cqlengine import connection

# Create the cluster, session
cluster = cluster()
session = cluster.connect()
session.set_keyspace('test')

# Register it as the default session
connection.register_connection('conn', session=session, default=True)

# Register UDT
cluster.register_user_type('test', 'myudt', mymodels.UDT)

# The UDTs are working as expected
# ...

# Error
myutilfuncs.run_query()

myutilfuncs.py

import mymodels
from cassandra.cqlengine import connection

def run_query():
  q = mymodels.MyTable.objects.filter(val=mymodels.UDT(foo='bar'))
  row = q.first() # cassandra.protocol.SyntaxException


  connection.cluster.register_user_type('test', 'myudt', UDT)
  q = mymodels.MyTable.objects.filter(val=mymodels.UDT(foo='bar'))
  row = q.first() # OK


Suggest a fix

Register the UDTs each time they are imported.

nigel5 avatar Apr 05 '24 03:04 nigel5

Latest python driver test run failed with the similar problem: Screenshot from 2024-04-22 14-07-26

vponomaryov avatar Apr 22 '24 11:04 vponomaryov

@vponomaryov I don't think your issue is related to the above issue description.

roydahan avatar May 01 '24 11:05 roydahan

Regarding the original description, it's more of a request to register the UDT when importing but I'm not sure it's a behaviour we would like to adopt.

roydahan avatar May 01 '24 11:05 roydahan