From b1a79d962c12fc245d9cf4a16e4c1ecb6103fccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Fri, 3 Jul 2026 10:46:14 +0800 Subject: [PATCH] fix: write_holding_strategy DELETE+INSERT (no UNIQUE constraint on code) --- mofin_db.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/mofin_db.py b/mofin_db.py index e33791b..f6dde63 100644 --- a/mofin_db.py +++ b/mofin_db.py @@ -169,7 +169,7 @@ def init_all_tables(conn: sqlite3.Connection): updated_at TEXT, superseded_at TEXT ); - CREATE INDEX IF NOT EXISTS idx_strategy_code ON holding_strategies(code); + CREATE UNIQUE INDEX IF NOT EXISTS idx_strategy_code ON holding_strategies(code); CREATE INDEX IF NOT EXISTS idx_strategy_status ON holding_strategies(status); -- 自选股 @@ -954,6 +954,8 @@ def write_holding_strategy(conn, code: str, name: str, data: dict) -> tuple[bool """写入持仓策略(替代 decisions.json 单条写入)。data 必须包含 currency。""" try: currency = data.get('currency', 'CNY') + # DELETE + INSERT(避免依赖 code 的 UNIQUE 约束) + conn.execute("DELETE FROM holding_strategies WHERE code=?", (code,)) conn.execute(""" INSERT INTO holding_strategies (code, name, version, price, cost, shares, stop_loss, take_profit, @@ -962,18 +964,6 @@ def write_holding_strategy(conn, code: str, name: str, data: dict) -> tuple[bool sector_context, status, trigger_json, changelog_json, source, reason, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,datetime('now','localtime')) - ON CONFLICT(code) DO UPDATE SET - name=excluded.name, price=excluded.price, cost=excluded.cost, - shares=excluded.shares, stop_loss=excluded.stop_loss, - take_profit=excluded.take_profit, entry_low=excluded.entry_low, - entry_high=excluded.entry_high, currency=excluded.currency, - action=excluded.action, timing_signal=excluded.timing_signal, - rr_ratio=excluded.rr_ratio, tech_snapshot=excluded.tech_snapshot, - stock_category=excluded.stock_category, - sector_context=excluded.sector_context, status=excluded.status, - trigger_json=excluded.trigger_json, changelog_json=excluded.changelog_json, - source=excluded.source, reason=excluded.reason, - updated_at=datetime('now','localtime') """, ( code, name, data.get('version', 1), data.get('price'), data.get('cost'),