SQLAlchemy: successful insert but then throws an exception

I run SQLAlchemy against FirebirdSQL, and when I execute a command insertin my project, SQLAlchemy throws an exception when returning from execution to the connection. However, the request insertis built and executed successfully. A database query shows that the items are actually correctly inserted.

Edit: Now I go into the module fbcore.py, and checking the value valuealso vartypemeans that the problem is probably due to how the element is used SEQUENCEto generate the primary key identifier, its data is returned. vartype SQL_LONG, but the actual value [<an integer>], where <an integer>is the value returned by the sequence generator I, created to automatically increase the primary key (for example, [14]). This suggests that the problem should be resolved by fixing this, although I'm not sure how to do it. The generator seems to work correctly inside the database itself, but causes problems when returning to SQLAlchemy.

See below for my existing implementation and stack trace.

My code is:

class Project:
    # (I've snipped project instantiation, where engine connection, table, etc. are configured)
    def save_project(self, id_=None, title=None, file_name=None, file_location=None):

        # Build the dictionary of values to store
        values = {}
        if title is not None:
            values['title'] = title

        if file_name is not None:
            values['file_name'] = file_name

        if file_location is not None:
            values['file_location'] = file_location

        # Simplification: I account for the case that there *is* data---skipping that here

        # Execute the correct kind of statement: insert or settings_update.
        if id_ is None:
            statement = self.table.insert()

        else:
            statement = self.table.update().where(self.table.c.id == id_)

        result = self.connection.execute(statement, values)

        # If we inserted a row, get the new primary key. Otherwise, return
        # the one specified by the user; it does not change on settings_update.
        project_id = result.inserted_primary_key if result.is_insert else id_

Track:

  File "/Users/chris/development/quest/workspace/my_project/data/tables.py", line 350, in save_project
    result = self.connection.execute(statement, values)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/engine/base.py", line 720, in execute
    return meth(self, multiparams, params)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/sql/elements.py", line 317, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/engine/base.py", line 817, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/engine/base.py", line 947, in _execute_context
    context)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/engine/base.py", line 1111, in _handle_dbapi_exception
    util.reraise(*exc_info)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/util/compat.py", line 168, in reraise
    raise value
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/engine/base.py", line 940, in _execute_context
    context)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/sqlalchemy/dialects/firebird/kinterbasdb.py", line 106, in do_execute
    cursor.execute(statement, parameters or [])
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/fdb/fbcore.py", line 3323, in execute
    self._ps._execute(parameters)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/fdb/fbcore.py", line 2991, in _execute
    self.__Tuple2XSQLDA(self._in_sqlda, parameters)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/fdb/fbcore.py", line 2782, in __Tuple2XSQLDA
    sqlvar.sqlscale)
  File "/Users/chris/.virtualenvs/my_project/lib/python3.3/site-packages/fdb/fbcore.py", line 2266, in _check_integer_range
    if (value < vmin) or (value > vmax):
TypeError: unorderable types: list() < int()

SQLAlchemy, , ; tutorial. , -, - , - dict, ? docs , , , - , - , , .

self.table.insert().values(values) , values execute ( ).

: , docstring execute fbcore.py TypeError, , , , . , ?

2: , , kinterbasdb, fdb. .

+4
2

, , , ​​, , UPDATE , . project_id ( ), ( ) , project_id, :

project_id = result.inserted_primary_key if result.is_insert else id_

:

project_id = result.inserted_primary_key[0] if result.is_insert else id_

SQLAlchemy ( ):

.

, .

, . ( : , .) , , .

, , Blinker - , c'est la vie...

+3

SQL Alchemy, , :

if id_ is None:
    statement = self.table.insert()

else:
    statement = self.table.update().where(self.table.c.id == id_)

statement = statement.values(title=title, file_name=file_name, file_location=file_location)

result = self.connection.execute(statement)

: , ( Insert Expressions).

+1

Source: https://habr.com/ru/post/1535046/


All Articles