fix: write_holding_strategy DELETE+INSERT (no UNIQUE constraint on code)

This commit is contained in:
知微
2026-07-03 10:46:14 +08:00
parent 6eecc4d4eb
commit b1a79d962c
+3 -13
View File
@@ -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'),