Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a difference between querying with Id and querying with two different attribute values? #1803

Open
YoonJeongLulu opened this issue Jun 10, 2024 · 1 comment

Comments

@YoonJeongLulu
Copy link

YoonJeongLulu commented Jun 10, 2024

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?

@KrisLau
Copy link
Contributor

KrisLau commented Aug 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants