2

我正在尝试使用 Delphi XE3 和 Zeos 7.0.4 在 MySQL 5.6 中保留两个具有主从关系的表。当我在主服务器上执行 ApplyUpdates 时,自动增量字段的值保持为 0。我需要自动增量值,因此我可以将明细表与来自 ApplyUpdates 的主表的 ID 字段链接起来。我正在使用 ZConnection,AutoCommit = FALSE 和 TransactionIsolationLevel = tiReadCommitted,ZQuery 和 CachedUpdates = TRUE。我错过了什么?

ZQPerson.Append;
ZQEmployee.Append;
try
  ZQPersonName.Value := Edit1.Text;
  ZQPerson.ApplyUpdates; //Here I expected to have the auto increment value on the Id field of ZQPerson, but it returns always 0
  ZQEmployeePersonID.Value := ZQPersonId.Value; //Here I'd link Employee to it's Person record
  ZQEmployeeRegNo.Value := StrToInt(Edit2.Text);
  ZQEmployee.ApplyUpdates;
  ZConnection1.Commit; //Here I would persist both tables in a single transaction to avoid master table without details
except
  ZQPerson.CancelUpdates;
  ZQEmployee.CancelUpdates;
  ZConnection1.Rollback; //In case of exceptions rollback everything
  raise;
end;
ZQPerson.CommitUpdates;
ZQEmployee.CommitUpdates;

我的 ZSQLMonitor 跟踪是这样的:

2013-08-29 00:01:23 cat: Execute, proto: mysql-5, msg: INSERT INTO person (Id, name) VALUES (NULL, 'Edit1') --> This is just after ZQPerson.ApplyUpdates
2013-08-29 00:01:50 cat: Execute, proto: mysql-5, msg: INSERT INTO employee (Id, RegNo, ProductId) VALUES (NULL, 1000, 0), errcode: 1452, error: Cannot add or update a child row: a foreign key constraint fails (`test`.`employee`, CONSTRAINT `FK_A6085E0491BDF8EE` FOREIGN KEY (`PersonId`) REFERENCES `person` (`Id`) --> This is just after ZQEmployee.ApplyUpdates
2013-08-29 00:02:05 cat: Execute, proto: mysql-5, msg: Native Rollback call --> Rollback after Exception on the ZQEmployee.ApplyUpdates
4

3 回答 3

2

您是否使用 ZConnection1.StartTransaction 开始事务?我也认为您必须在调用 ZQuery1.ApplyUpdates 后刷新 ZQuery1 以获取新的 id-

阅读您的评论,您必须在没有 where 子句的情况下执行 select *?正确的?我可以推荐你使用这种方法:

1) 选择并增加当前的自动增量值
2) 从主表中选择其中 id=[step1 id] // 它将为空,当然
3) 在步骤 1 中使用 id 添加详细信息
4) 在主数据集中分配 id
5) 应用两个更新

于 2013-08-29T01:28:59.233 回答
0

我找到的解决方法就是这个。它不能完全满足我,因为它没有使数据库的自动增量功能的使用变得透明,使我使用 Last_Insert_ID() 函数。我正在与 zeos 开发人员联系以检查这一点。

function LastInsertID(ATableName: string): Integer;
var DBQuery: TZQuery;
begin
  DBQuery := TZQuery.Create(Self);
  with DBQuery do
  begin
    Connection := ZConnection1;
    SQL.Clear;
    SQL.Add('Select Last_Insert_ID() as Last_Insert_ID from ' + ATableName);
    Open;
    Result := FieldByName('Last_Insert_ID').Value;
    Free;
  end;
end;

procedure Persist;
var LastID: Integer;
begin
  ZQPerson.Append;
  ZQEmployee.Append;
  try
    ZQPersonName.Value := Edit1.Text;
    ZQPerson.ApplyUpdates; // Here I expected to have the auto increment value on the Id field of ZQPerson, but it returns always 0
    LastID := LastInsertID('Person'); //Getting the Last_Insert_ID(), even on the uncommitted transction, works
    ZQEmployeePersonId.Value := LastID; //Link the two tables using the Last_Insert_ID() result
    ZQEmployeeRegNo.Value := StrToInt(Edit2.Text);
    ZQEmployee.ApplyUpdates;
    ZConnection1.Commit; // Here I persist both tables in a single transaction to avoid master table without details
  except
    ZQPerson.CancelUpdates;
    ZQEmployee.CancelUpdates;
    ZConnection1.Rollback; // In case of exceptions rollback everything
    raise;
  end;
  ZQPerson.CommitUpdates;
  ZQEmployee.CommitUpdates;
于 2013-08-29T20:27:52.493 回答
0

我在一个简单的数据库中测试了它,其中有两个主表和明细表,它们嵌套在 TDataSource 中,并由明细表的 关联where

object conMysql: TZConnection
     TransactIsolationLevel = tiReadCommitted
object zqryMaster: TZQuery
     Connection = conMysql
SQL.Strings = (
       'select * from temp.master')
object dsNestedMaster: TDataSource
     DataSet = zqryMaster
object zqryDetail: TZQuery
     Connection = conMysql
     SQL.Strings = (
       'select * from temp.detail'
       'where id_master =: id')

启动事务后,所有更新必须等待确认或发生错误时回滚:

  try
    zqryMaster.Connection.StartTransaction;
    zqryMaster.Edit;
    zqryDetail.Edit;
    zqryMaster.FindField('dt_mov').Value := Now;
    while not zqryDetail.Eof do
    begin
      zqryDetail.Edit;
      zqryDetail.FindField('dt_mov').Value := Now;
      zqryDetail.ApplyUpdates;
      zqryDetail.Next;
      //raise Exception.Create('simple error'); //use for tests, check database after perform
    end;
    zqryMaster.ApplyUpdates;
    zqryMaster.Connection.Commit;
  except
    zqryMaster.Connection.Rollback;
    zqryMaster.CancelUpdates;
    zqryDetail.CancelUpdates;
  end;
于 2020-04-11T20:06:10.543 回答