Skip to content

Commit

Permalink
Fix invalid SQL code for dropping foreign key constraint in PostgreSQ…
Browse files Browse the repository at this point in the history
…L mode. Closes #1247
  • Loading branch information
ansgarbecker committed Mar 28, 2024
1 parent 6434e2f commit fcca03a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion source/dbconnection.pas
Expand Up @@ -428,7 +428,8 @@ TDBLogItem = class(TObject)
spUSEQuery, spKillQuery, spKillProcess,
spFuncLength, spFuncCeil, spFuncLeft, spFuncNow, spFuncLastAutoIncNumber,
spLockedTables, spDisableForeignKeyChecks, spEnableForeignKeyChecks,
spOrderAsc, spOrderDesc);
spOrderAsc, spOrderDesc,
spForeignKeyDrop);

TDBConnection = class(TComponent)
private
Expand Down Expand Up @@ -3081,6 +3082,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spLockedTables] := '';
FSQLSpecifities[spDisableForeignKeyChecks] := 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0';
FSQLSpecifities[spEnableForeignKeyChecks] := 'SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1)';
FSQLSpecifities[spForeignKeyDrop] := 'DROP FOREIGN KEY %s';
end;
ngMSSQL: begin
FSQLSpecifities[spDatabaseDrop] := 'DROP DATABASE %s';
Expand All @@ -3105,6 +3107,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spLockedTables] := '';
FSQLSpecifities[spDisableForeignKeyChecks] := '';
FSQLSpecifities[spEnableForeignKeyChecks] := '';
FSQLSpecifities[spForeignKeyDrop] := 'DROP FOREIGN KEY %s';
end;
ngPgSQL: begin
FSQLSpecifities[spDatabaseDrop] := 'DROP SCHEMA %s';
Expand All @@ -3131,6 +3134,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spLockedTables] := '';
FSQLSpecifities[spDisableForeignKeyChecks] := '';
FSQLSpecifities[spEnableForeignKeyChecks] := '';
FSQLSpecifities[spForeignKeyDrop] := 'DROP CONSTRAINT %s';
end;
ngSQLite: begin
FSQLSpecifities[spDatabaseDrop] := 'DROP DATABASE %s';
Expand All @@ -3156,6 +3160,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spLockedTables] := '';
FSQLSpecifities[spDisableForeignKeyChecks] := '';
FSQLSpecifities[spEnableForeignKeyChecks] := '';
FSQLSpecifities[spForeignKeyDrop] := 'DROP FOREIGN KEY %s';
end;
ngInterbase: begin
FSQLSpecifities[spDatabaseDrop] := 'DROP DATABASE %s';
Expand Down Expand Up @@ -3184,6 +3189,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spLockedTables] := '';
FSQLSpecifities[spDisableForeignKeyChecks] := '';
FSQLSpecifities[spEnableForeignKeyChecks] := '';
FSQLSpecifities[spForeignKeyDrop] := 'DROP FOREIGN KEY %s';
end;

end;
Expand Down
7 changes: 4 additions & 3 deletions source/table_editor.pas
Expand Up @@ -600,7 +600,7 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
// ALTER TABLE statement. Separate statements are required."
for i:=0 to FForeignKeys.Count-1 do begin
if FForeignKeys[i].Modified and (not FForeignKeys[i].Added) then
Specs.Add('DROP FOREIGN KEY '+Conn.QuoteIdent(FForeignKeys[i].OldKeyName));
Specs.Add(DBObject.Connection.GetSQLSpecifity(spForeignKeyDrop, [Conn.QuoteIdent(FForeignKeys[i].OldKeyName)]));
end;
FinishSpecs;

Expand Down Expand Up @@ -805,8 +805,9 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
Specs.Add('ADD '+FKeys[i].SQLCode);
end;

for i:=0 to FDeletedForeignKeys.Count-1 do
Specs.Add('DROP FOREIGN KEY '+Conn.QuoteIdent(FDeletedForeignKeys[i]));
for i:=0 to FDeletedForeignKeys.Count-1 do begin
Specs.Add(DBObject.Connection.GetSQLSpecifity(spForeignKeyDrop, [Conn.QuoteIdent(FDeletedForeignKeys[i])]));
end;
for i:=0 to FForeignKeys.Count-1 do begin
if FForeignKeys[i].Added or FForeignKeys[i].Modified then
Specs.Add('ADD '+FForeignKeys[i].SQLCode(True));
Expand Down

0 comments on commit fcca03a

Please sign in to comment.