浏览代码

Fix backend startup: stop using alter:true for SQLite sync

Sequelize's alter:true on SQLite drops and recreates tables to change
columns, which fails with a FK constraint error when other tables
reference the one being altered (e.g. DROP TABLE users). Beyond the FK
error, dropping live tables is unsafe regardless.

Switch to plain sync() which only creates tables that don't exist and
never touches existing schema. Schema migrations for new columns must
be handled explicitly if needed in the future.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
k4be 12 小时之前
父节点
当前提交
b6d88abb99
共有 1 个文件被更改,包括 4 次插入1 次删除
  1. 4 1
      gpx-vis-backend/src/database.js

+ 4 - 1
gpx-vis-backend/src/database.js

@@ -31,7 +31,10 @@ async function initDatabase() {
   // Import models to register them
   require('./models');
   const isMemory = (config.database?.storage === ':memory:');
-  await sq.sync(isMemory ? {} : { alter: true });
+  // sync() creates missing tables without touching existing ones.
+  // alter:true is intentionally avoided: on SQLite it drops and recreates
+  // tables to change columns, which destroys data and breaks FK constraints.
+  await sq.sync(isMemory ? {} : {});
   console.log('Database initialized');
 }