omnizart icon indicating copy to clipboard operation
omnizart copied to clipboard

数据管理下载数据,导入postgre数据库时,peewee报错ON CONFLICT DO UPDATE

Open linst95 opened this issue 1 year ago • 1 comments

环境

  • 操作系统: Windows 11
  • VeighNa版本: VeighNa Trader 社区版 - 3.9.0

Issue类型

Bug

预期程序行为

从交易所下载数据,并导入到postgre数据库

实际程序行为

数据成功下载,但是导入到postgre时报错

重现步骤

在数据管理中,下载数据

Traceback (most recent call last): File "C:\veighna_studio\lib\site-packages\peewee.py", line 3251, in execute_sql cursor.execute(sql, params or ()) psycopg2.errors.CardinalityViolation: ON CONFLICT DO UPDATE command cannot affect row a second time HINT: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\veighna_studio\lib\site-packages\vnpy_datamanager\ui\widget.py", line 610, in download count: int = self.engine.download_bar_data(symbol, exchange, interval, start, self.output) File "C:\veighna_studio\lib\site-packages\vnpy_datamanager\engine.py", line 215, in download_bar_data self.database.save_bar_data(data) File "C:\veighna_studio\lib\site-packages\vnpy_postgresql\postgresql_database.py", line 199, in save_bar_data ).execute() File "C:\veighna_studio\lib\site-packages\peewee.py", line 1971, in inner return method(self, database, *args, **kwargs) File "C:\veighna_studio\lib\site-packages\peewee.py", line 2042, in execute return self._execute(database) File "C:\veighna_studio\lib\site-packages\peewee.py", line 2847, in _execute return super(Insert, self)._execute(database) File "C:\veighna_studio\lib\site-packages\peewee.py", line 2558, in _execute cursor = self.execute_returning(database) File "C:\veighna_studio\lib\site-packages\peewee.py", line 2565, in execute_returning cursor = database.execute(self) File "C:\veighna_studio\lib\site-packages\peewee.py", line 3259, in execute return self.execute_sql(sql, params) File "C:\veighna_studio\lib\site-packages\peewee.py", line 3249, in execute_sql with exception_wrapper: File "C:\veighna_studio\lib\site-packages\peewee.py", line 3019, in exit reraise(new_type, new_type(exc_value, *exc_args), traceback) File "C:\veighna_studio\lib\site-packages\peewee.py", line 192, in reraise raise value.with_traceback(tb) File "C:\veighna_studio\lib\site-packages\peewee.py", line 3251, in execute_sql cursor.execute(sql, params or ()) peewee.ProgrammingError: ON CONFLICT DO UPDATE command cannot affect row a second time HINT: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

image

linst95 avatar Jan 19 '24 02:01 linst95

解决办法很奇怪,将chunk size从100调到10,之后没有再报错: for c in chunked(data, 10):

linst95 avatar Jan 19 '24 05:01 linst95

这个报错应该是插入的数据中有重复key(这个应用场景下应该是datetime重复了)导致的,因为插入数据是无序的,数据库没法判断哪条数据是最新的

noranhe avatar Mar 15 '24 05:03 noranhe