Is there a difference between querying with Id and querying with two different attribute values?
I constructed the token model as Watermelon's model as follows. The id is set to be generated automatically.
tableSchema({
name: TABLE_NAMES.TOKEN,
columns: [
{
name: TOKEN_TABLE_COLUMNS.CHAIN_ID,
type: ColumnTypes.NUMBER,
isIndexed: true,
},
{
name: TOKEN_TABLE_COLUMNS.CONTRACT_ADDRESS,
type: ColumnTypes.STRING,
isOptional: true,
isIndexed: true,
},
{ name: TOKEN_TABLE_COLUMNS.NAME, type: ColumnTypes.STRING },
...,
}),
And I made a function to query with two attributes as below, contactAddress, ChainId, and if it already exists, I don't add it to the DB, but if it doesn't, I add it.
addTokenModel: async ({ chainID, contractAddress }) => {
const tokenModel = await TokenRepository.getTokenModel({
contractAddress,
chainID,
});
if (tokenModel) {
throw err;
}
return TokenRepository.addTokenModel({
chainID,
contractAddress,
...,
});
},
However, if the addTokenModel function is continuously executed asynchronously, it is not possible to find a model that already exists in the same DB, but write to the DB, and in conclusion, the id is different, but a row with the same chainId and contactAddress occurs.
That is, when the first run and the second run occur at the same time, it doesn't seem to refer to the tokenModel that has already been created in the second run.
However, if '${chainId}-${contactAddress}' is designated as id and searched as id, this is not the case. Even after continuous testing, there was no error that occurred when the id was overwritten twice.
I already left chainId and contactAddress isIndexed when I designed the schema.
But I wonder why this difference occurred. Is this a coincidence?
It shouldn't have a difference if your methods are get method is working properly (awaiting all the queries properly etc) but I'm not 100% sure. You would probably need to post the code for anyone to be able to help debug but ideally just try logging every step of all our methods and you should be able to find the part causing the issue