fix: write_holding_strategy DELETE+INSERT (no UNIQUE constraint on code)
This commit is contained in:
+3
-13
@@ -169,7 +169,7 @@ def init_all_tables(conn: sqlite3.Connection):
|
|||||||
updated_at TEXT,
|
updated_at TEXT,
|
||||||
superseded_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);
|
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。"""
|
"""写入持仓策略(替代 decisions.json 单条写入)。data 必须包含 currency。"""
|
||||||
try:
|
try:
|
||||||
currency = data.get('currency', 'CNY')
|
currency = data.get('currency', 'CNY')
|
||||||
|
# DELETE + INSERT(避免依赖 code 的 UNIQUE 约束)
|
||||||
|
conn.execute("DELETE FROM holding_strategies WHERE code=?", (code,))
|
||||||
conn.execute("""
|
conn.execute("""
|
||||||
INSERT INTO holding_strategies
|
INSERT INTO holding_strategies
|
||||||
(code, name, version, price, cost, shares, stop_loss, take_profit,
|
(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,
|
sector_context, status, trigger_json, changelog_json,
|
||||||
source, reason, updated_at)
|
source, reason, updated_at)
|
||||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,datetime('now','localtime'))
|
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,
|
code, name,
|
||||||
data.get('version', 1), data.get('price'), data.get('cost'),
|
data.get('version', 1), data.get('price'), data.get('cost'),
|
||||||
|
|||||||
Reference in New Issue
Block a user