diff --git a/source/copytable.pas b/source/copytable.pas index 60d28ff3..749162f3 100644 --- a/source/copytable.pas +++ b/source/copytable.pas @@ -390,7 +390,7 @@ begin DataCols := DataCols + MainForm.mask(FColumns[Node.Index].Name) + ', '; end; nKeys: Clause := FKeys[Node.Index].SQLCode; - nForeignkeys: Clause := FForeignKeys[Node.Index].SQLCode; + nForeignkeys: Clause := FForeignKeys[Node.Index].SQLCode(False); else raise Exception.Create(SUnhandledNodeIndex); end; CreateCode := CreateCode + #9 + Clause + ',' + CRLF; diff --git a/source/mysql_connection.pas b/source/mysql_connection.pas index d5710f23..d834713a 100644 --- a/source/mysql_connection.pas +++ b/source/mysql_connection.pas @@ -88,7 +88,7 @@ type Modified, Added, KeyNameWasCustomized: Boolean; constructor Create; destructor Destroy; override; - function SQLCode: String; + function SQLCode(IncludeSymbolName: Boolean): String; end; TForeignKeyList = TObjectList; @@ -2560,11 +2560,15 @@ begin inherited Destroy; end; -function TForeignKey.SQLCode: String; +function TForeignKey.SQLCode(IncludeSymbolName: Boolean): String; var i: Integer; begin - Result := 'CONSTRAINT '+TMySQLConnection.QuoteIdent(KeyName)+' FOREIGN KEY ('; + Result := ''; + // Symbol names are unique in a db. In order to autocreate a valid name we leave the constraint clause away. + if IncludeSymbolName then + Result := 'CONSTRAINT '+TMySQLConnection.QuoteIdent(KeyName)+' '; + Result := Result + 'FOREIGN KEY ('; for i:=0 to Columns.Count-1 do Result := Result + TMySQLConnection.QuoteIdent(Columns[i]) + ', '; if Columns.Count > 0 then Delete(Result, Length(Result)-1, 2); diff --git a/source/table_editor.pas b/source/table_editor.pas index ba57ec52..4a7503b2 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -579,7 +579,7 @@ begin Specs.Add('DROP FOREIGN KEY '+Mainform.mask(DeletedForeignKeys[i])); for i:=0 to FForeignKeys.Count-1 do begin if FForeignKeys[i].Added or FForeignKeys[i].Modified then - Specs.Add('ADD '+FForeignKeys[i].SQLCode); + Specs.Add('ADD '+FForeignKeys[i].SQLCode(True)); end; Result := 'ALTER TABLE '+Mainform.mask(DBObject.Name) + CRLF + #9 + ImplodeStr(',' + CRLF + #9, Specs); @@ -617,7 +617,7 @@ begin end; for i:=0 to FForeignKeys.Count-1 do - Result := Result + #9 + FForeignKeys[i].SQLCode + ','+CRLF; + Result := Result + #9 + FForeignKeys[i].SQLCode(True) + ','+CRLF; if Integer(listColumns.RootNodeCount) + IndexCount + FForeignKeys.Count > 0 then Delete(Result, Length(Result)-2, 3);