Python : Sqlalchemy Batch Insert With On_conflict_update
I have to insert approx. 30000 rows daily in my postgres database, I have 4 columns in my database namely : id(pkey), category, createddate, updatedon. My requirement is to update
Solution 1:
From your comment
id_tag is nothing but mane of my table in postgres
one could deduce that id_tag
is bound to a string. If you'd provided a Minimal, Complete, and Verifiable example, there'd been a lot less guesswork. As it turns out, postgresql.dml.insert()
automatically wraps passed strings in a text()
construct, and the result when trying to use Insert.excluded
is:
In [2]: postgresql.insert('fail').excluded
~/sqlalchemy/lib/sqlalchemy/sql/selectable.py:43: SAWarning: Textual SQL FROM expression 'fail' should be explicitly declared astext('fail'), or usetable('fail') formorespecificity (this warning may be suppressed after 10 occurrences)
{"expr": util.ellipses_string(element)})
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-f176aac8b913> in <module>()
---->1 postgresql.insert('fail').excluded
~/sqlalchemy/lib/sqlalchemy/util/langhelpers.py in __get__(self, obj, cls)
765if obj is None:
766returnself
-->767 obj.__dict__[self.__name__] = result = self.fget(obj)
768return result
769
~/sqlalchemy/lib/sqlalchemy/dialects/postgresql/dml.py inexcluded(self)
4142"""
---> 43 return alias(self.table, name='excluded').columns
44
45 @_generative
~/sqlalchemy/lib/sqlalchemy/sql/selectable.py in alias(selectable, name, flat)
159
160 """
-->161return _interpret_as_from(selectable).alias(name=name, flat=flat)
162163
AttributeError: 'TextClause' object has no attribute 'alias'
So, instead of passing a string containing the name of your table to postgresql.dml.insert()
pass it an actual Table
object, or a light weight table()
construct that has been populated with column()
objects.
Post a Comment for "Python : Sqlalchemy Batch Insert With On_conflict_update"