Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
8eeb102
1
Parent(s): 3adb25c
Fix table_id constraint and data restoration
Browse files
app.py
CHANGED
|
@@ -115,7 +115,7 @@ class Participant(db.Model):
|
|
| 115 |
email = db.Column(db.String(120), nullable=False)
|
| 116 |
fb_link = db.Column(db.String(200))
|
| 117 |
introduction = db.Column(db.Text)
|
| 118 |
-
table_id = db.Column(db.Integer, db.ForeignKey('table.id')
|
| 119 |
seat_number = db.Column(db.Integer)
|
| 120 |
is_table_leader = db.Column(db.Boolean, default=False) # 新增: 是否為桌長
|
| 121 |
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
@@ -320,7 +320,7 @@ def restore_settings():
|
|
| 320 |
)
|
| 321 |
db.session.add(table)
|
| 322 |
db.session.flush() # 獲取新的 table.id
|
| 323 |
-
tables[table_data['id']] = table.id
|
| 324 |
|
| 325 |
# 恢復參與者資料
|
| 326 |
for participant_data in data['participants']:
|
|
@@ -342,12 +342,19 @@ def restore_settings():
|
|
| 342 |
# 如果都失敗,使用當前時間
|
| 343 |
created_at = datetime.utcnow()
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
participant = Participant(
|
| 346 |
nickname=participant_data['nickname'],
|
| 347 |
email=participant_data['email'],
|
| 348 |
fb_link=participant_data.get('fb_link'),
|
| 349 |
introduction=participant_data.get('introduction'),
|
| 350 |
-
table_id=
|
| 351 |
seat_number=participant_data.get('seat_number'),
|
| 352 |
is_table_leader=participant_data.get('is_table_leader', False),
|
| 353 |
created_at=created_at
|
|
|
|
| 115 |
email = db.Column(db.String(120), nullable=False)
|
| 116 |
fb_link = db.Column(db.String(200))
|
| 117 |
introduction = db.Column(db.Text)
|
| 118 |
+
table_id = db.Column(db.Integer, db.ForeignKey('table.id')) # 移除 nullable=False
|
| 119 |
seat_number = db.Column(db.Integer)
|
| 120 |
is_table_leader = db.Column(db.Boolean, default=False) # 新增: 是否為桌長
|
| 121 |
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
|
|
| 320 |
)
|
| 321 |
db.session.add(table)
|
| 322 |
db.session.flush() # 獲取新的 table.id
|
| 323 |
+
tables[str(table_data['id'])] = table.id # 將 id 轉換為字串
|
| 324 |
|
| 325 |
# 恢復參與者資料
|
| 326 |
for participant_data in data['participants']:
|
|
|
|
| 342 |
# 如果都失敗,使用當前時間
|
| 343 |
created_at = datetime.utcnow()
|
| 344 |
|
| 345 |
+
# 確保 table_id 存在且有效
|
| 346 |
+
old_table_id = str(participant_data['table_id']) # 將 id 轉換為字串
|
| 347 |
+
new_table_id = tables.get(old_table_id)
|
| 348 |
+
|
| 349 |
+
if new_table_id is None:
|
| 350 |
+
print(f'Warning: Invalid table_id {old_table_id} for participant {participant_data["nickname"]}')
|
| 351 |
+
|
| 352 |
participant = Participant(
|
| 353 |
nickname=participant_data['nickname'],
|
| 354 |
email=participant_data['email'],
|
| 355 |
fb_link=participant_data.get('fb_link'),
|
| 356 |
introduction=participant_data.get('introduction'),
|
| 357 |
+
table_id=new_table_id, # 可能為 None
|
| 358 |
seat_number=participant_data.get('seat_number'),
|
| 359 |
is_table_leader=participant_data.get('is_table_leader', False),
|
| 360 |
created_at=created_at
|