Repository navigation
Closed
Labels
bugSomething isn't working
Description
Describe the bug
When using attachRow() to create a relationship between two records, the function executes successfully outside of the context of a transaction but fails when executed as part a transaction. The error is not particularly helpful:
Insights listening on port 8081
Server default listening on port 8080
Webserver listening on port 8082
METHOD CALL: internal.addDataLimetrack duration: 19199ms numQueries: 0 authenticatedUser: 3
DatabaseException: Failed to update row, no rows updated
#0 DatabaseConnection.updateRow (package:serverpod/src/database/adapters/postgres/database_connection.dart:226:7)
<asynchronous suspension>
#1 AddressAttachRowRepository.country (package:limetrack_server/src/generated/address.dart:508:5)
<asynchronous suspension>
#2 InternalEndpoint.addDataLimetrack.<anonymous closure> (package:limetrack_server/src/endpoints/internal_endpoint.dart:802:7)
<asynchronous suspension>
#3 PgConnectionImplementation.runTx.<anonymous closure> (package:postgres/src/v3/connection.dart:480:24)
<asynchronous suspension>
#4 Pool.withResource (package:pool/pool.dart:127:14)
<asynchronous suspension>
#5 PoolImplementation.withConnection (package:postgres/src/pool/pool_impl.dart:153:16)
<asynchronous suspension>
#6 Database.transaction (package:serverpod/src/database/database.dart:291:12)
<asynchronous suspension>
#7 InternalEndpoint.addDataLimetrack (package:limetrack_server/src/endpoints/internal_endpoint.dart:800:5)
<asynchronous suspension>
#8 EndpointDispatch.handleUriCall (package:serverpod/src/server/endpoint_dispatch.dart:127:20)
<asynchronous suspension>
#9 Server._handleRequest (package:serverpod/src/server/server.dart:296:18)
<asynchronous suspension>
To Reproduce
Steps to reproduce the behavior:
- Create a project with a simple one-to-many relationship
- Insert the rows required to create the records
- Use attachRow() to create the relationship between the records
- Repeat steps 2 and 3 inside a transaction
- See error
Expected behavior
I expected to be able to create the necessary relationships - using attachRow - within the context of a transaction. It's important to be able to use a transaction in order to rollback a series of inserts if anything goes wrong.
Serverpod versions
- CLI Version: Serverpod version: 2.0.1
- Library version:
serverpod: 2.0.1
serverpod_auth_server: 2.0.1
serverpod_serialization: 2.0.1
Platform information
Server running in development mode on an Apple MacBook M1. Standard Docker deployment.
Code snippet
// Lookups
CompanyType? companyType = await CompanyType.db.findFirstRow(
session,
where: (t) => t.name.ilike('%private company%'),
);
Country? country = await Country.db.findFirstRow(
session,
where: (t) => t.name.ilike('%united kingdom%'),
);
// Data
Address address = Address(line1: 'Address line 1', town: 'Town', postcode: 'PostCode');
Carrier carrier = Carrier(companyName: 'Company Name');
// Add data without transaction - WORKS
Address newAddress = await Address.db.insertRow(session, address);
await Address.db.attachRow.country(session, newAddress, country!);
Carrier newCarrier = await Carrier.db.insertRow(session, carrier);
await Carrier.db.attachRow.companyType(session, newCarrier, companyType!);
await Carrier.db.attachRow.registeredAddress(session, newCarrier, newAddress);
// Add data with transaction - FAILS
await session.db.transaction((transaction) async {
Address newTxnAddress = await Address.db.insertRow(session, address, transaction: transaction);
// This call fails with the above exception
await Address.db.attachRow.country(session, newTxnAddress, country!);
Carrier newTxnCarrier = await Carrier.db.insertRow(session, carrier, transaction: transaction);
await Carrier.db.attachRow.companyType(session, newTxnCarrier, companyType!);
await Carrier.db.attachRow.registeredAddress(session, newCarrier, newTxnAddress);
});
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
CordMemescape commentedon Jul 6, 2024
I've retested the above, replacing the attachRow() calls with updateRow(), with the same results. The updateRow() call fails inside the transaction with the following error. The failure is unsurprising because attachRow() simply calls update under the hood.
SandPod commentedon Jul 8, 2024
Hey @CordMemescape! 👋
Just a simple sanity check question: do you get this error even if you remove the queries performed outside the transaction? (I just want to validate that there are no index restrictions on the table that prevent multiple addresses from being connected to the same country).
Could you provide the minimum models required to reproduce this issue? That would help us when debugging.
CordMemescape commentedon Jul 8, 2024
Hey @SandPod! Yes, the error happens whether those queries are inside or outside of the transaction. In my original code, they were all done inside the transaction and I only moved them out for brevity and so that I could include the code that worked along with the code that didn't.
Initially I thought that the issue might be because of nested transactions, particularly where I was dealing with lists. I have several functions that accept lists as parameters and attempt to perform the inserts in the context of transactions so that if anything fails, I can roll the whole lot back. I stripped everything out to the most basic example that I could, just to confirm that the issue still happens. And it does.
Since posting this, I went back to take a closer look the
attach()andattachRow()functions. They callupdate<T>()under the hood but do not use a transaction parameter. Therefore, since the inserts are done within the context of the transaction, the attaches will not be able to 'see' the changes until they are committed. In other words, the foreign keys referenced byattach()/attachRow()do not exist at the time the functions are called. This will cause an exception which is what I suspect we are seeing. One solution might be to commit the changes before callingattach()/attachRow()but this may not be desirable, especially if the attach is merely part of a larger transaction that you really only want to commit right at the very end - when everything has been done successfully.Here are the four main models used in the above.
SandPod commentedon Jul 8, 2024
@CordMemescape Thank you for the additional information.
I think your findings are correct and we are missing the transaction parameter for the generate attach methods. Here is a snippet of a generated class in the test project where the optional transaction parameter is missing:
serverpod/tests/serverpod_test_server/lib/src/generated/long_identifiers/deep_includes/city_with_long_table_name.dart
Lines 527 to 551 in 9f9f04f
Luckily this is something we can fix as a non breaking change.