@@ -638,7 +638,18 @@ export class SqlDriver implements Driver {
638638 } ) ;
639639 }
640640 } else if ( this . config . client === 'sqlite3' ) {
641- // SQLite PRAGMA doesn't support parameter binding, so we need to ensure safe identifier
641+ // SQLite PRAGMA doesn't support parameter binding, so we need to ensure safe identifier.
642+ // First, verify that the requested table actually exists using a parameterized query.
643+ const tableExistsResult = await this . knex . raw (
644+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?" ,
645+ [ tableName ]
646+ ) ;
647+
648+ if ( ! tableExistsResult || tableExistsResult . length === 0 ) {
649+ // If the table does not exist, there are no foreign keys to introspect.
650+ return foreignKeys ;
651+ }
652+
642653 // Table names in ObjectQL are validated and should be safe, but we add extra protection
643654 const safeTableName = tableName . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '' ) ;
644655 const result = await this . knex . raw ( `PRAGMA foreign_key_list(${ safeTableName } )` ) ;
@@ -653,7 +664,7 @@ export class SqlDriver implements Driver {
653664 }
654665 }
655666 } catch ( error ) {
656- console . warn ( ` Could not introspect foreign keys for table ${ tableName } :` , error ) ;
667+ console . warn ( ' Could not introspect foreign keys for requested table:' , error ) ;
657668 }
658669
659670 return foreignKeys ;
@@ -693,8 +704,17 @@ export class SqlDriver implements Driver {
693704 }
694705 } else if ( this . config . client === 'sqlite3' ) {
695706 // SQLite PRAGMA doesn't support parameter binding, so we need to ensure safe identifier
696- // Table names in ObjectQL are validated and should be safe, but we add extra protection
697707 const safeTableName = tableName . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '' ) ;
708+
709+ // Validate that the sanitized table name exists in the database before using it in PRAGMA
710+ const tablesResult = await this . knex . raw ( "SELECT name FROM sqlite_master WHERE type = 'table'" ) ;
711+ const tableNames = tablesResult . map ( ( row : any ) => row . name ) ;
712+
713+ if ( ! tableNames . includes ( safeTableName ) ) {
714+ console . warn ( 'Could not introspect primary keys for SQLite table: table does not exist after sanitization.' ) ;
715+ return primaryKeys ;
716+ }
717+
698718 const result = await this . knex . raw ( `PRAGMA table_info(${ safeTableName } )` ) ;
699719
700720 for ( const row of result ) {
@@ -704,7 +724,7 @@ export class SqlDriver implements Driver {
704724 }
705725 }
706726 } catch ( error ) {
707- console . warn ( ` Could not introspect primary keys for table ${ tableName } :` , error ) ;
727+ console . warn ( ' Could not introspect primary keys for a table:' , error ) ;
708728 }
709729
710730 return primaryKeys ;
0 commit comments