crate / sqlalchemy-cratedb

SQLAlchemy dialect for CrateDB.
https://cratedb.com/docs/sqlalchemy-cratedb/
Apache License 2.0
3 stars 2 forks source link

Cannot insert data into a table having only one object column #131

Closed lsynchr closed 8 years ago

lsynchr commented 8 years ago

Here is my POCs.

# poc1.py
from crate import client

server = "127.0.0.1:4200"

def crate_python_test():
    connection = client.connect(server)
    cursor = connection.cursor()
    cursor.execute("""CREATE TABLE test_1(i int, o object)""")
    cursor.execute("""INSERT INTO test_1(i, o) VALUES(?, ?)""", (0, {'db' : 'Crate'}))
    # It's OK.
    cursor.execute("""CREATE TABLE test_2(o object)""")
    cursor.execute("""INSERT INTO test_2(o) VALUES(?)""", ({'db' : 'Crate'}))
    # It will raise an exception.
    # crate.client.exceptions.ProgrammingError: SQLActionException[Failed to parse source [{"args": {"db": "Crate"}, "stmt": "INSERT INTO test_2(o) VALUES(?)"}]]

crate_python_test()
# poc2.py
import sqlalchemy

server = "127.0.0.1:4200"

def sqlalchemy_test():
    engine = sqlalchemy.create_engine('crate://' + server)
    connect = engine.connect()
    connect.execute("""CREATE TABLE test_1(i int, o object)""")
    connect.execute("""INSERT INTO test_1(i, o) VALUES(?, ?)""", (0, {'db' : 'Crate'}))
    # It's OK.
    connect.execute("""CREATE TABLE test_2(o object)""")
    connect.execute("""INSERT INTO test_2(o) VALUES(?)""", ({'db' : 'Crate'}))
    # It will raise an exception.
    # sqlalchemy.exc.ProgrammingError: (crate.client.exceptions.ProgrammingError) SQLActionException[Failed to parse source [{"args": {"db": "Crate"}, "stmt": "INSERT INTO test_2(o) VALUES(?)"}]] [SQL: 'INSERT INTO test_2(o) VALUES(?)'] [parameters: {'db': 'Crate'}]

sqlalchemy_test()
seut commented 8 years ago

you missed out that a tuple with a single value must be written like (item,) in python, see e.g.: https://wiki.python.org/moin/TupleSyntax.

so in your POC, this is the correct syntax/usage:

cursor.execute("""INSERT INTO test_2(o) VALUES(?)""", ({'db' : 'Crate'},))