fix: SQLite vector table now respects OM_VECTOR_TABLE env variable - #80
Conversation
- SQLite was hardcoding table name as 'vectors' while docker-compose sets OM_VECTOR_TABLE=openmemory_vectors by default - This caused 'no such table: openmemory_vectors' error on fresh installs - PostgreSQL already respected this env variable, now SQLite does too - Default remains 'vectors' for backward compatibility with existing DBs
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where the SQLite backend was ignoring the OM_VECTOR_TABLE environment variable, causing "no such table" errors in fresh Docker installations. The fix ensures SQLite respects the environment variable while maintaining backward compatibility with existing databases.
- Reads
OM_VECTOR_TABLEenvironment variable withvectorsas default for backward compatibility - Updates SQLite table creation and index creation to use the configurable table name
- Aligns SQLite behavior with PostgreSQL, which already respected this environment variable
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ); | ||
| db.run( | ||
| `create table if not exists vectors(id text not null,sector text not null,user_id text,v blob not null,dim integer not null,primary key(id,sector))`, | ||
| `create table if not exists ${sqlite_vector_table}(id text not null,sector text not null,user_id text,v blob not null,dim integer not null,primary key(id,sector))`, |
There was a problem hiding this comment.
The table name from the environment variable is directly interpolated into the SQL statement without proper escaping or validation. While PostgreSQL uses quoted identifiers (line 92), SQLite does not, potentially allowing SQL injection if OM_VECTOR_TABLE contains malicious input like vectors; DROP TABLE memories--.
Consider either:
- Adding validation to ensure the table name contains only safe characters (alphanumeric and underscores)
- Using quoted identifiers similar to PostgreSQL:
`create table if not exists "${sqlite_vector_table}"(...)`
Example validation:
const sqlite_vector_table = process.env.OM_VECTOR_TABLE || "vectors";
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(sqlite_vector_table)) {
throw new Error(`Invalid table name: ${sqlite_vector_table}`);
}| ); | ||
| db.run( | ||
| "create index if not exists idx_vectors_user on vectors(user_id)", | ||
| `create index if not exists idx_vectors_user on ${sqlite_vector_table}(user_id)`, |
There was a problem hiding this comment.
The table name from the environment variable is directly interpolated into the SQL statement without proper escaping or validation. This creates the same SQL injection vulnerability as the table creation statement.
Apply the same validation or escaping approach as recommended for the table creation to ensure consistency and security.
Summary
Fixes SQLite backend ignoring the
OM_VECTOR_TABLEenvironment variable, causing "no such table" errors on fresh Docker installs.Problem
docker-compose.ymlsetsOM_VECTOR_TABLE=openmemory_vectorsby defaultvectors(ignoring the env variable)openmemory_vectors(from env)SQLITE_ERROR: no such table: openmemory_vectorsPostgreSQL already respected this env variable correctly.
Solution
Make SQLite table creation and index use the
OM_VECTOR_TABLEenv variable, withvectorsas the default for backward compatibility with existing databases.Changes
backend/src/core/db.ts:OM_VECTOR_TABLEenv variable (default:vectors)Testing
vectorstable still work (backward compatible)