Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

UpgradeError Not yet support for changing primary Key -- with 3.0.0-rc.7

Open jokamax opened this issue 4 years ago • 11 comments

Hi,

Sorry I haven't log because this is a user remark. I migrated from : 3.0.0-rc.2 to 3.0.0-rc.7 And now, a user sent me a mail with the problem : OpenFailedError;UpgradeError Not yet support for changing primary Key

Is it not corrected in 3.0.0-rc.7 ??

Thanks,

Jonathan

jokamax avatar Apr 21 '20 13:04 jokamax

Yea #1004 and was resolved in rc.6. There might be another issue. Would it be possible to get a repro?

dfahlander avatar Apr 21 '20 21:04 dfahlander

Tried to trigger this bug with various browsers and configs but the only way I've been able to do it, is by changing the actual primary key, such as renaming its property, or changing auto-incrementation flag. It seems to work as expected for both inbound and outbound keys.

Could it be that your user for some reason had a very old version that was not compatible? Or had a another database with the same name at the same origin? Just trying to figure out possible reason. Dexie prior to 3.0.0-rc.3 would not detect this problem but pretend everything is normal until you try a request on the primary index.

dfahlander avatar Apr 21 '20 21:04 dfahlander

First of all : I will ask the user the "exact" configuration of his system (browser and version).

Then, I will try to test to reproduce.

If I can't... Then I will "wait" another user to have the problem (because for the user, which encounter the problem I ask him to use a drop database function I wrote in my app, which drop and reload local from server -> after : everything OK).

Dexie prior to 3.0.0-rc.3 would not detect this problem but pretend everything is normal until you try a request on the primary index. ==> Maybe I've always had the problem without catching it properly ;)

jokamax avatar Apr 22 '20 05:04 jokamax

Encountered this error today! In my case, I am removing the auto-increment from primary key. Because I am converting the PK to UUID.

Is this type of upgrade not supported? else I am thinking of piping the data to a new table and then might delete the old table in next upgrade? Will this be good enough?

raevilman avatar Sep 18 '21 17:09 raevilman

Autoincrement is only a fallback. It's still allowed to set UUIDs as primary keys on autoincremented tables. So you could basically leave it as autoincremented if you just keep the same name of the property of the primary key.

If that feels odd or you must change which property to use as primary key, there's an example here: You need to do it in 3 versions with upgraders on each: https://github.com/dfahlander/Dexie.js/issues/781#issuecomment-443237897

dfahlander avatar Sep 18 '21 20:09 dfahlander

Update: I've actually refactored my code as per your suggestion here i.e. without dropping auto-increment flag and its working so far...


PS: Thank you for all your work!

raevilman avatar Sep 18 '21 20:09 raevilman

Really confused by this - is it simpler if I am willing to delete the table in an upgrade? Had no idea something simple like this would require anything more than simply changing the schema and updating the version. Seems like dexie.js should handle this process behind the scenes rather than users having to implement the abomination mentioned here and elsewhere?

josephrocca avatar Mar 31 '23 10:03 josephrocca

Really confused by this - is it simpler if I am willing to delete the table in an upgrade? Had no idea something simple like this would require anything more than simply changing the schema and updating the version. Seems like dexie.js should handle this process behind the scenes rather than users having to implement the abomination mentioned here and elsewhere?

The process of changing primary key could be improved and there are lots of ideas around this. It's not supported natively though and there are a few unanswered questions on how what the developer would expect from changing the primary key property:

  1. Should the database fail to open if there are one or more existing rows that lacks the new primary key?
  2. Should the database fail to open if there are two rows containing the same value for primary key?
  3. Would it be expected by dexie to copy the old prop into the new or would that be the responsibility of the developer to add an upgrader to do this?

dfahlander avatar Mar 31 '23 11:03 dfahlander

Good questions - do you know what MongoDB and other popular databases do in those cases? IIRC MongoDB:

  1. Uses 'undefined' as the index value if it's missing in a doc/row
  2. Fails when trying to create a new unique index if there's a key collision for that property

Not sure what you mean by point 3 - isn't the dev just specifying changes in indexes/uniqueness? Why would that involve any copying of values?

josephrocca avatar Mar 31 '23 11:03 josephrocca

Any chance that Dexie.js v4 will have the ability to change primary key? This would be really helpful. I've only just started using Dexie in the last couple of months and I've hit into this problem 3 times now.

josephrocca avatar May 14 '23 16:05 josephrocca

Hello, I'd like to necro this and ask what is the support for changing primary keys. The last messages are from 10 months ago.

Thanks

gsabater avatar Feb 02 '24 13:02 gsabater