mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 19:11:39 -06:00
Allow changing columns when foreign keys are enabled and used
When foreign keys are enabled it used to be impossible to change a column of a table which is referenced in a foreign key. This is because the table gets dropped in the process of renaming the column and only then replaced by a new version. With this commit the foreign keys are disabled for this short period of time where the table doesn't exist and the old settings value is restored as soon as the renaming is done. See issue #362.
This commit is contained in:
@@ -963,6 +963,10 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq
|
||||
otherObjectsSql += (*it).getsql() + "\n";
|
||||
}
|
||||
|
||||
// Store the current foreign key settings and then disable the foreign keys being enforced to make sure the table can be dropped without errors
|
||||
QString foreignKeysOldSettings = getPragma("foreign_keys");
|
||||
setPragma("foreign_keys", "0");
|
||||
|
||||
// Delete the old table
|
||||
if(!executeSQL(QString("DROP TABLE `%1`;").arg(tablename)))
|
||||
{
|
||||
@@ -980,6 +984,9 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq
|
||||
return false;
|
||||
}
|
||||
|
||||
// Restore the former foreign key settings
|
||||
setPragma("foreign_keys", foreignKeysOldSettings);
|
||||
|
||||
// Restore the saved triggers, views and indices
|
||||
if(!executeMultiSQL(otherObjectsSql, true, true))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user