From 6ee7e1cd16205753774f932ec964aa0da6e7ca63 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Thu, 2 Oct 2025 10:30:59 -0700 Subject: [PATCH] Fix: Correct D1 schema creation and improve votes table --- functions/api/schema.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/functions/api/schema.js b/functions/api/schema.js index 0423263..86b5ca9 100644 --- a/functions/api/schema.js +++ b/functions/api/schema.js @@ -43,9 +43,12 @@ const schemaV1 = [ comment_id INTEGER, direction INTEGER NOT NULL CHECK(direction IN (1, -1)), created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY(user_id) REFERENCES users(id), + FOREIGN KEY(post_id) REFERENCES posts(id), + FOREIGN KEY(comment_id) REFERENCES comments(id), + CHECK ((post_id IS NOT NULL AND comment_id IS NULL) OR (post_id IS NULL AND comment_id IS NOT NULL)), UNIQUE(user_id, post_id), - UNIQUE(user_id, comment_id), - FOREIGN KEY(user_id) REFERENCES users(id) + UNIQUE(user_id, comment_id) );` ]; @@ -61,13 +64,12 @@ export async function onRequestPost({ request, env }) { } if (action === 'create') { - const results = await db.exec(schemaV1.join('')); + const results = await db.exec(schemaV1.join('\n')); return Response.json({ success: true, results }); } return Response.json({ success: false, error: 'Invalid action' }, { status: 400 }); } catch (e) { - const { message, cause } = e; - return Response.json({ success: false, error: { message, cause } }, { status: 500 }); + return Response.json({ success: false, error: e.message }, { status: 500 }); } }