Repository: promptfoo/promptfoo
Issue: Internal Server Error when sharing the result of eval to self-hosted server
Root cause: Validation Gap

**Describe the bug**
Internal Server Error when sharing the result of eval to self-hosted server

**To Reproduce**

1. Run self-hosted server with docker [self-hosting](https://www.promptfoo.dev/docs/usage/self-hosting/) (`0.92.2`)
2. Run promptfoo for eval (npx promptfoo eval) (`0.92.2`)
3. Share the results (npx promptfoo share -y)

**Expected behavior**
Sharing should be succeeded without any errors

**Screenshots**

- error message
```
$ npx promptfoo share -y
/home/abc/node_modules/promptfoo/dist/src/share.js:41
        throw new Error(`Failed to send eval results: ${response.statusText}`);
              ^

Error: Failed to send eval results: Internal Server Error
    at sendEvalResults /home/abc/node_modules/promptfoo/dist/src/share.js:41:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async createShareableUrl (/home/abc/node_modules/promptfoo/dist/src/share.js:100:18)
    at async createPublicUrl (/home/abc/prompt-pioneer/node_modules/promptfoo/dist/src/commands/share.js:20:17)
    at async Command.<anonymous> (/home/abc/node_modules/promptfoo/dist/src/commands/share.js:50:13)

Node.js v19.9.0

```
- log
```
Failed to write eval to database SqliteError: FOREIGN KEY constraint failed
    at PreparedQuery.run (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:88:22)
    at QueryPromise.run (/app/node_modules/drizzle-orm/sqlite-core/query-builders/insert.cjs:166:28)
    at Function.<anonymous> (/app/dist/src/models/eval.js:124:22)
    at Function.sqliteTransaction (/app/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at BetterSQLiteSession.transaction (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:56:51)
    at BaseSQLiteDatabase.transaction (/app/node_modules/drizzle-orm/sqlite-core/db.cjs:306:25)
    at Eval.create (/app/dist/src/models/eval.js:109:18)
    at /app/dist/src/server/server.js:144:52
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/app/node_modules/express/lib/router/route.js:149:13) {
  code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
...
} 
```

**System information (please complete the following information):**

- Promptfoo version: `0.92.2`

**Additional context**
It had worked well until `0.88.0`. I think it is to be related to the introduction of [EvaluateSummaryV3](https://www.promptfoo.dev/docs/configuration/reference/#evaluatesummary) at 0.92.0.

I tried even in a completely clean environment. but results were same.
What can I do for this?

Let me look into it for you

Same happens to me with version 0.93. 
I want to add that the command `npx promptfoo delete eval all` also throws the same type of error:  `SqliteError: FOREIGN KEY constraint failed`

The error in the remote instance:
```
2024-10-15T18:03:43.807789141Z Failed to write eval to database SqliteError: FOREIGN KEY constraint failed
2024-10-15T18:03:43.807835642Z     at PreparedQuery.run (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:88:22)
2024-10-15T18:03:43.807842842Z     at QueryPromise.run (/app/node_modules/drizzle-orm/sqlite-core/query-builders/insert.cjs:166:28)
2024-10-15T18:03:43.807855342Z     at Function.<anonymous> (/app/dist/src/models/eval.js:124:22)
2024-10-15T18:03:43.807860942Z     at Function.sqliteTransaction (/app/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
2024-10-15T18:03:43.807866042Z     at BetterSQLiteSession.transaction (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:56:51)
2024-10-15T18:03:43.807871042Z     at BaseSQLiteDatabase.transaction (/app/node_modules/drizzle-orm/sqlite-core/db.cjs:306:25)
2024-10-15T18:03:43.807876343Z     at Eval.create (/app/dist/src/models/eval.js:109:18)
2024-10-15T18:03:43.807881243Z     at /app/dist/src/server/server.js:144:52
2024-10-15T18:03:43.807886143Z     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2024-10-15T18:03:43.807891043Z     at next (/app/node_modules/express/lib/router/route.js:149:13) {
2024-10-15T18:03:43.807895843Z   code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
```

@sjsinju is this resolved for you?


> Same happens to me with version 0.93. I want to add that the command `npx promptfoo delete eval all` also throws the same type of error: `SqliteError: FOREIGN KEY constraint failed`
> 
> The error in the remote instance:
> 
> ```
> 2024-10-15T18:03:43.807789141Z Failed to write eval to database SqliteError: FOREIGN KEY constraint failed
> 2024-10-15T18:03:43.807835642Z     at PreparedQuery.run (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:88:22)
> 2024-10-15T18:03:43.807842842Z     at QueryPromise.run (/app/node_modules/drizzle-orm/sqlite-core/query-builders/insert.cjs:166:28)
> 2024-10-15T18:03:43.807855342Z     at Function.<anonymous> (/app/dist/src/models/eval.js:124:22)
> 2024-10-15T18:03:43.807860942Z     at Function.sqliteTransaction (/app/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
> 2024-10-15T18:03:43.807866042Z     at BetterSQLiteSession.transaction (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:56:51)
> 2024-10-15T18:03:43.807871042Z     at BaseSQLiteDatabase.transaction (/app/node_modules/drizzle-orm/sqlite-core/db.cjs:306:25)
> 2024-10-15T18:03:43.807876343Z     at Eval.create (/app/dist/src/models/eval.js:109:18)
> 2024-10-15T18:03:43.807881243Z     at /app/dist/src/server/server.js:144:52
> 2024-10-15T18:03:43.807886143Z     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
> 2024-10-15T18:03:43.807891043Z     at next (/app/node_modules/express/lib/router/route.js:149:13) {
> 2024-10-15T18:03:43.807895843Z   code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
> ```

Fix is up here: https://github.com/promptfoo/promptfoo/pull/1891

@sklein12 that was quick!
When your fix would see the light so we can update and use it?

Btw, this project is very useful, good work!

> @sklein12 that was quick! When your fix would see the light so we can update and use it?
> 
> Btw, this project is very useful, good work!

just pushed a release for you, v0.93.1 will have the fix

Thanks a lot! :)

@sklein12 I tried this morning with the new version 0.93.1 in both my PC and in the remote app (Docker container) and the command to share evals (`npx promptfoo share -y`) keeps failing:

Error in the command:

```
promptfoo\dist\src\share.js:45
        throw new Error(`Failed to send eval results: ${response.statusText}`);
              ^

Error: Failed to send eval results: Internal Server Error
    at sendEvalResults (C:\Users\aaa\AppData\Roaming\npm\node_modules\promptfoo\dist\src\share.js:45:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async createShareableUrl (C:\Users\aaa\AppData\Roaming\npm\node_modules\promptfoo\dist\src\share.js:104:18)
    at async createPublicUrl (C:\Users\aaa\AppData\Roaming\npm\node_modules\promptfoo\dist\src\commands\share.js:20:17)
    at async Command.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\promptfoo\dist\src\commands\share.js:50:13)
```

Error log in the web app running remotely:
```
2024-10-16T09:48:14.776662369Z Server running at http://localhost:3000 and monitoring for new evals.
2024-10-16T09:48:14.786388393Z Migrated results from file system to database

2024-10-16T11:07:34.352582654Z Failed to write eval to database SqliteError: FOREIGN KEY constraint failed
2024-10-16T11:07:34.352630355Z     at PreparedQuery.run (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:88:22)
2024-10-16T11:07:34.352637455Z     at QueryPromise.run (/app/node_modules/drizzle-orm/sqlite-core/query-builders/insert.cjs:166:28)
2024-10-16T11:07:34.352642655Z     at Function.<anonymous> (/app/dist/src/models/eval.js:124:22)
2024-10-16T11:07:34.352648655Z     at Function.sqliteTransaction (/app/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
2024-10-16T11:07:34.352653955Z     at BetterSQLiteSession.transaction (/app/node_modules/drizzle-orm/better-sqlite3/session.cjs:56:51)
2024-10-16T11:07:34.352658955Z     at BetterSQLite3Database.transaction (/app/node_modules/drizzle-orm/sqlite-core/db.cjs:312:25)
2024-10-16T11:07:34.352663956Z     at Eval.create (/app/dist/src/models/eval.js:109:18)
2024-10-16T11:07:34.352668856Z     at /app/dist/src/server/server.js:145:52
2024-10-16T11:07:34.352673756Z     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2024-10-16T11:07:34.352678756Z     at next (/app/node_modules/express/lib/router/route.js:149:13) {
2024-10-16T11:07:34.352683656Z   code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
```

@sjsinju I think this should do it.. https://github.com/promptfoo/promptfoo/pull/1902

I'll let you know when we cut a release.

@sjsinju can you pull the latest and see if that resolves your issue?

> @sjsinju can you pull the latest and see if that resolves your issue?

I tried to run with both `0.93.1` version and `0.93.2` version.
I also still have the same problem at `0.93.1` version likes the case of [alex-pollan](https://github.com/alex-pollan). But the version of `0.93.2` works well. I think it was resolved. Thanks :)

yes guys, it works now in 0.93.2+
Thanks!