Fix sync errors: string move_id, FK constraints on connection pool

API returns move_id as string ("Season-013-Week-11-1772073335"), not
i64. Also disable foreign_keys at pool level via SqliteConnectOptions
since transactions reference players/teams that may not exist locally.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-02-28 10:50:27 -06:00
parent a18c0431d1
commit 60b397b529
3 changed files with 8 additions and 12 deletions

View File

@ -185,7 +185,6 @@ pub async fn sync_transactions(
continue;
};
let move_id_str = move_id.to_string();
let player_id = player.id;
// Schema has from_team_id/to_team_id as NOT NULL; use 0 as sentinel for missing teams
let from_team_id = data.oldteam.map(|t| t.id).unwrap_or(0);
@ -206,7 +205,7 @@ pub async fn sync_transactions(
)
.bind(season)
.bind(week)
.bind(move_id_str)
.bind(&move_id)
.bind(player_id)
.bind(from_team_id)
.bind(to_team_id)

View File

@ -147,9 +147,9 @@ pub struct TransactionsResponse {
#[derive(Debug, Deserialize)]
pub struct TransactionData {
/// Transaction move ID — API field is "moveid".
/// Transaction move ID — API field is "moveid" (string like "Season-013-Week-11-1772073335").
#[serde(rename = "moveid", default)]
pub move_id: Option<i64>,
pub move_id: Option<String>,
#[serde(default)]
pub week: Option<i64>,
#[serde(default)]

View File

@ -7,7 +7,8 @@ pub async fn init_pool(db_path: &Path) -> Result<SqlitePool> {
let db_url = format!("sqlite://{}?mode=rwc", db_path.display());
let options = SqliteConnectOptions::from_str(&db_url)?
.create_if_missing(true)
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
.foreign_keys(false);
let pool = SqlitePoolOptions::new()
.max_connections(5)
@ -18,10 +19,6 @@ pub async fn init_pool(db_path: &Path) -> Result<SqlitePool> {
}
pub async fn create_tables(pool: &SqlitePool) -> Result<()> {
sqlx::query("PRAGMA foreign_keys = ON")
.execute(pool)
.await?;
// 1. teams — API-provided PKs (no autoincrement)
sqlx::query(
"CREATE TABLE IF NOT EXISTS teams (
@ -175,9 +172,9 @@ pub async fn create_tables(pool: &SqlitePool) -> Result<()> {
season INTEGER NOT NULL,
week INTEGER NOT NULL,
move_id TEXT NOT NULL,
player_id INTEGER NOT NULL REFERENCES players(id),
from_team_id INTEGER NOT NULL REFERENCES teams(id),
to_team_id INTEGER NOT NULL REFERENCES teams(id),
player_id INTEGER NOT NULL,
from_team_id INTEGER NOT NULL,
to_team_id INTEGER NOT NULL,
cancelled INTEGER DEFAULT 0,
frozen INTEGER DEFAULT 0,
synced_at TEXT,