scylladb / python-driver

ScyllaDB Python Driver, originally DataStax Python Driver for Apache Cassandra
https://python-driver.docs.scylladb.com
Apache License 2.0
75 stars 42 forks source link

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

Closed nigel5 closed 6 months ago

nigel5 commented 7 months ago

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.

vponomaryov commented 6 months ago

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

roydahan commented 6 months ago

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

roydahan commented 6 months ago

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.