{"record":{"id":"37c4d035ddff187d","repo":"phalcon/cphalcon","slug":"dropping-a-foreign-key-constraint-is-not-supported","errorCode":null,"errorMessage":"Dropping a foreign key constraint is not supported by SQLite","messagePattern":"Dropping a foreign key constraint is not supported by SQLite","errorType":"exception","errorClass":"Phalcon\\Db\\Exceptions\\SqliteDropForeignKeyNotSupported","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Dialect/Sqlite.zep","lineNumber":374,"sourceCode":"    {\n        return \"ALTER TABLE \" . this->prepareTable(tableName, schemaName)\n            . \" DROP COLUMN \\\"\" . columnName . \"\\\"\";\n    }\n\n    /**\n     * SQLite cannot DROP a CHECK constraint from an existing table.\n     */\n    public function dropCheck( string tableName,  string schemaName,  string checkName) -> string\n    {\n        throw new SqliteDropCheckNotSupported();\n    }\n\n    /**\n     * Generates SQL to delete a foreign key from a table\n     */\n    public function dropForeignKey( string tableName,  string schemaName,  string referenceName) -> string\n    {\n        throw new SqliteDropForeignKeyNotSupported();\n    }\n\n    /**\n     * Generates SQL to delete an index from a table\n     */\n    public function dropIndex( string tableName,  string schemaName,  string indexName) -> string\n    {\n        if schemaName {\n            return \"DROP INDEX \\\"\" . schemaName . \"\\\".\\\"\" . indexName . \"\\\"\";\n        }\n\n        return \"DROP INDEX \\\"\" . indexName . \"\\\"\";\n    }\n\n    /**\n     * Generates SQL to delete primary key from a table\n     */\n    public function dropPrimaryKey( string tableName,  string schemaName) -> string","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Dialect/Sqlite.zep#L356-L392","documentation":"Phalcon\\Db\\Dialect\\Sqlite::dropForeignKey() always throws SqliteDropForeignKeyNotSupported. SQLite enforces foreign keys but cannot drop them from an existing table; the constraint is part of the CREATE TABLE definition, so removing it requires a full table rebuild.","triggerScenarios":"Shared migration code calling $connection->dropForeignKey('posts', null, 'fk_posts_user') on a connection whose dialect is Sqlite; migration steps written for MySQL executed unchanged in SQLite-based test setups.","commonSituations":"Test suites on :memory: SQLite while production runs MySQL/PostgreSQL; refactoring tools that drop and re-add constraints; multi-tenant products supporting multiple database backends with one migration path.","solutions":["Branch on $connection->getDialectType() and skip the call for 'sqlite'","Rebuild the table without the FOREIGN KEY clause: CREATE TABLE new ..., INSERT INTO new SELECT * FROM old, DROP TABLE old, ALTER TABLE new RENAME TO old","Model the constraint change as a new table version in a dialect-aware migration instead of an ALTER"],"exampleFix":"// before\n$connection->dropForeignKey('posts', null, 'fk_posts_user');\n\n// after\nif ($connection->getDialectType() === 'sqlite') {\n    // rebuild table without the FOREIGN KEY clause, then copy data and rename\n} else {\n    $connection->dropForeignKey('posts', null, 'fk_posts_user');\n}","handlingStrategy":"try-catch","validationCode":"if ($connection->getDialectType() !== 'sqlite') {\n    $connection->dropForeignKey('posts', null, 'fk_posts_user');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $connection->dropForeignKey('posts', null, 'fk_posts_user');\n} catch (\\Phalcon\\Db\\Exceptions\\SqliteDropForeignKeyNotSupported $e) {\n    // SQLite: rebuild table without the FOREIGN KEY clause, copy data, rename\n    $logger->info('Skipped dropForeignKey on SQLite', ['table' => 'posts']);\n}","preventionTips":["Branch schema-alter steps on $connection->getDialectType()","On SQLite, treat FK changes as table rebuilds in one batched migration step","Keep a matrix of which ALTER operations each target dialect supports"],"tags":["sqlite","phalcon-db","ddl","foreign-key","unsupported-operation","migrations"],"backgroundTag":"sqlite-alter-limitation","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}