SQLalchemy语句构建在on_duplicate_key_update()

时间:2019-03-17 19:02:05

标签: sqlalchemy python-3.6 mariadb-connect-engine

我正在尝试使sqlalchemy(1.3)on_duplicate_key_update()工作并失败。

我什至不知道出了什么问题。这是示例代码和跟踪,请告知:

from sqlalchemy.dialects.mysql import insert
import sqlalchemy as sa
import mysql.connector

engine=sa.create_engine('mysql+mysqldb://user:pass@[::1]db')
metadata = sa.MetaData()

# If you have access to a db, 
# just uncomment the following to create a testing table:

# metadata.reflect(bind=engine)
# numbers = sa.Table('numbers', metadata,
#     sa.Column('id', sa.Integer, primary_key=True),
#     sa.Column('number', sa.String(32), unique=True)
# )
# metadata.create_all(engine)

metadata.reflect(bind=engine)
conn=engine.connect()

# https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#insert-on-duplicate-key-update-upsert
ins=insert(metadata.tables['numbers']).values({'number':"one"})
on_dup=ins.on_duplicate_key_update({'number':"two"})

r=conn.execute(on_dup)
print(r.fetchone())

打印时(on_dup)

  

AttributeError跟踪(最近的调用)   持续)   E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ visitors.py   在_compiler_dispatch中(自己,访客,** kw)        86试试:   ---> 87 meth = getter(访客)        88除了AttributeError:

AttributeError: 'StrSQLCompiler' object has no attribute 'visit_on_duplicate_key_update'

During handling of the above exception, another exception occurred:

UnsupportedCompilationError
           Traceback (most recent call
  

最后一个)        18 ins = insert(metadata.tables ['numbers'])。values({'number':12})        19 on_dup = ins.on_duplicate_key_update({'number':2})   -> 20次打印(on_dup)        21 con.execute(on_dup)

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ elements.py   在 str (自己)       470 def str (自己):       471如果util.py3k:   -> 472 return str(self.compile())       其他473:       第474章      

在(自身,绑定,方言,** kw)中

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ elements.py   在编译中(self,default,bind,方言,** kw)       460其他:       461方言= default.StrCompileDialect()   -> 462返回self._compiler(方言,bind = bind,** kw)       463       464 def _compiler(自己,方言,** kw):

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ elements.py   在_compiler中(自己,方言,** kw)       466种方言。“”“       467   -> 468返回方言.statement_compiler(方言,自我,** kw)       469       470 def str (自己):

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ compiler.py   在 init 中(自身,方言,语句,column_keys,内联,** kwargs)       560#Dialect.label_length或Dialect.max_identifier_length       561 self.truncated_names = {}   -> 562已编译。初始化(自身,方言,陈述,**变态)       563       第564章;      

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ compiler.py   在 init (自身,方言,语句,绑定,schema_translate_map,   compile_kwargs)       317如果self.can_execute:       318 self.execution_options = statement._execution_options   -> 319 self.string = self.process(self.statement,** compile_kwargs)       320       321 @ util.deprecated(

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ compiler.py   进行中(自己,obj,** kwargs)       348       349 def进程(self,obj,** kwargs):   -> 350返回obj._compiler_dispatch(self,** kwargs)       351       352 def str (自己):

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ visitors.py   在_compiler_dispatch中(自己,访客,** kw)        89引发exc.UnsupportedCompilationError(visitor,cls)        其他90个:   ---> 91 return meth(self,** kw)        92        其他93个:

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ compiler.py   在visit_insert(self,insert_stmt,asfrom,** kw)中2458如果   insert_stmt._post_values_clause不是None:2459
  post_values_clause = self.process(   -> 2460 insert_stmt._post_values_clause,** kw 2461)2462如果post_values_clause:

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ compiler.py   进行中(自己,obj,** kwargs)       348       349 def进程(self,obj,** kwargs):   -> 350返回obj._compiler_dispatch(self,** kwargs)       351       352 def str (自己):

     

E:\ Coding \ Anaconda3 \ envs \ tf \ lib \ site-packages \ sqlalchemy \ sql \ visitors.py   在_compiler_dispatch中(自己,访客,** kw)        87 meth = getter(访客)        88除了AttributeError:   ---> 89引发exc.UnsupportedCompilationError(visitor,cls)        其他90个:        91 return meth(self,** kw)

UnsupportedCompilationError: Compiler
<sqlalchemy.sql.compiler.StrSQLCompiler object at 0x0000025A48769A90>
can't render element of type <class sqlalchemy.dialects.mysql.dml.OnDuplicateClause'>

并在conn.execute(on_dup)上得到result.fetchone()的NoneType

我正在从conda-forge使用sqlalchemy 1.3.1连接到MariaDB 10.3.13。在具有Python 3.6.7的jupyter Notebook v5.7.6中运行

0 个答案:

没有答案