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

Composite Primary Keys broken on replicated collections #4190

Closed
wants to merge 4 commits into from

Conversation

peterkogo
Copy link

@peterkogo peterkogo commented Dec 21, 2022

BUG REPLICATION

Describe the problem you have without this PR

When documents are added to a collection with a composite primary key through the pull handler of a replication, the resulting composite primary key is accessed before it is generated.

@peterkogo
Copy link
Author

This is a work in progress - I wanted to check if the unit tests actually work. I hope it is ok that I already opened the pull request.

@peterkogo
Copy link
Author

@pubkey the bug reproduction is finished.
I tried to look for a possible fix, however I lack a deeper grasp of the replication flow to be confident about my reasoning.

@pubkey
Copy link
Owner

pubkey commented Dec 26, 2022

Test looks ok, however this is intentional. RxDB expects the remote to only send documents that exactly match the schema, so the primary key cannot be missing.
I would merge a feature to fill up the primary key if possible, but I will not do the work by myself.
If we fill up missing keys, we also need a check to ensure that an error is thrown if the composite primary key is there but different than how RxDB would create it.

@peterkogo
Copy link
Author

I find it odd that the behaviour is different for data coming through replication vs. data coming through insert(). What is the reasoning behind this?

I thought the logic behind supporting composite primary keys in general was to make it compatible with backend data models - in my case a postgres database - but the composite key behaviour of the backend is not directly compatible with rxdb (concatinating two strings with a separator).
I think it is bad design to expect the remote data/api to reflect an implementation that is configured and only relevant on the client.

Anyways, I'd be interested to add this if you can give me a little hint where the change would make sense.
Otherwise I'll make a pull request to add additional documentation on the current state of affairs. Right now it sounds like the key added for the composite primary is solely there for rxdb to fill it in.

@pubkey
Copy link
Owner

pubkey commented Dec 26, 2022

You are totally right, the current behavior is not correct. I just wanted to tell you that, so you do not have to look at error at your side.

Anyways, I'd be interested to add this if you can give me a little hint where the change would make sense.

If you want to fix that, it should be done in the replication plugin. There we already transform all data that comes from the server. Atm it only replaces the custom deleted field with _deleted. This is done with the function swapdeletedFieldToDefaultDeleted(). So you can check the parts of the code where this function is used, and there it should also check/fill-up the primary key. The solution will be just a few lines of code, but building the correct tests could be some work to do.

@pubkey pubkey closed this in 3c44534 Jan 9, 2023
@peterkogo
Copy link
Author

@pubkey I just wanted to start working on this and saw you closed this with a fix! Thank you!
Do you still need some help with additional tests?

@pubkey
Copy link
Owner

pubkey commented Jan 11, 2023

@peterkey
I just added your single test.
We could need an additional test (or enhance the current one) to ensure the composite key is filled when the document comes from the pull.stream$.

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

Successfully merging this pull request may close these issues.

2 participants