Hey AlexP,
I did some more testing on this issue and it seems that there is a problem with the declaration
NOT NULL on fields that are not primary key autoincrement. There are two problems:
- Non-direct mode on android does not return autoincrement value
- Direct mode insertion and update on one entity in a transaction fails with "no such file" exception
Both errors do occur if a field of the table is declared as
NOT NULL. The primary key autoincrement
NOT NULL seems to be ok.
Here a code example to reproduce the two errors:
Code: Select all
var
table: TUniTable;
id: Integer;
begin
MyConnection.ExecSQL('DROP TABLE IF EXISTS tbl_Name');
// A: this DDL SUCCEEDS
MyConnection.ExecSQL('CREATE TABLE tbl_Name (ID_AutoInc INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, SomeField INTEGER)');
// B: this DDL FAILS
MyConnection.ExecSQL('CREATE TABLE tbl_Name (ID_AutoInc INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, SomeField INTEGER NOT NULL)');
MyConnection.StartTransaction;
try
table := TUniTable.Create(nil);
try
table.Connection := MyConnection;
table.TableName := 'tbl_Name';
table.CachedUpdates := True;
table.Options.RequiredFields := False;
table.Open;
table.Append;
table.FieldByName('SomeField').AsInteger := 42;
table.ApplyUpdates();
ShowMessage('ID_AutoInc Insert: ' + table.FieldByName('ID_AutoInc').AsInteger.ToString());
id := table.FieldByName('ID_AutoInc').AsInteger;
finally
table.Free;
end;
if id = 0 then exit; // we got no id...
table := TUniTable.Create(nil);
try
table.Connection := MyConnection;
table.TableName := 'tbl_Name';
table.CachedUpdates := True;
table.Options.RequiredFields := False;
table.Open;
if not table.Locate('ID_AutoInc', id, []) then raise Exception.CreateFmt('Cannot find entity %d', [id]);
table.Edit;
table.FieldByName('SomeField').AsInteger := 43;
table.ApplyUpdates();
ShowMessage('ID_AutoInc Update: ' + table.FieldByName('ID_AutoInc').AsInteger.ToString());
finally
table.Free;
end;
MyConnection.Commit;
except
MyConnection.Rollback;
raise;
end;
You can comment out A or B to let the example fail or succeed.
Execute the example on android.
With DDL A:
-> You get two messages: "ID_AutoInc Insert: 1" and "ID_AutoInc Update: 1"
With DDL B:
-> On non-direct mode, you get the message "ID_AutoInc Insert: 0".
-> On direct mode, you get an error "no such file or directory"