如何使用asyncpg(pgsql)获取“返回ID”

时间:2019-05-20 14:04:53

标签: python python-3.x postgresql asyncpg

使用asyncpg在Python中执行insert语句,并使用返回ID语法获取每个插入的ID,但返回“ INSERT 0 1”

Python 3.5

asyncpg(使用连接池)

enter image description here

1 个答案:

答案 0 :(得分:1)

[TestMethod] public void CreateCustomer_Successfully() { // IsolationLevel.RepeatableRead has to be same as the IsolationLevel in the CustomerService.Handle method. using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead })) { using (var context = ContextHelper.DatabaseContext()) { // Get number of rows in database. customerRowsBeforeCreation = context.Customer.Count(); // Arrange: Save Original Customer into database. customerRepository.Save(customer); // Arrange CopyCustomer command. var copyCustomerCommand = new CopyCustomer(){...}; // Act customerService.Handle(copyCustomerCommand); // Now i need to get number of customer rows in the database. // But the last customer insert was not commited, because it is a test. // So i probably need to call uncommited read, but anyway this row does not end, // it is waiting infinitely. // Get number of rows in database. headerRowsAfterCopy = context.Header.Count(); } // No scope.Complete => Rollback // scope.Complete(); } // Assert Assert.Equals(0, exceptions.Count()); Assert.Equals(headerRowsBeforeCopy, headerRowsAfterCopy - 1); } (及其变体)可用于返回任何数据返回语句的结果,包括Connection.fetch()