lucid icon indicating copy to clipboard operation
lucid copied to clipboard

fix: compare DateTime in newUpIfMissing

Open aadamcik opened this issue 1 year ago • 4 comments

🔗 Linked issue

#1018

❓ Type of change

  • [x] 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

Resolves #1018

Fixes bug in updateOrCreateMany, fetchOrCreateMany and fetchOrNewUpMany (which uses newUpIfMissing) where it does not compare DateTime values correctly and therefore creates duplicate rows. https://github.com/adonisjs/lucid/blob/develop/src/orm/base_model/index.ts#L212

📝 Checklist

  • [x] I have linked an issue or discussion.

aadamcik avatar Jan 27 '24 09:01 aadamcik

Hey! 👋🏻

Thanks for your contribution! It seems that the fail, may you link into it?

RomainLanz avatar Mar 26 '24 21:03 RomainLanz

@RomainLanz updated. I had to change transformValue method so drivers get Date object instead in queries.

aadamcik avatar Mar 28 '24 20:03 aadamcik

I see there is a bigger problem here because the newUpIfMissing will fail for any sort of any non-literal values.

Yes, we might be able to fix it for DateTime instances, but the same check will fail if some property holds an array of values or an object.

thetutlage avatar Mar 29 '24 03:03 thetutlage

@thetutlage @RomainLanz I managed to get it working for all dialects. The comments are incorporated.

However this is not the cleanest solution when it comes to transforming the DateTime values, it should be ok for now. I think it would be best to transform all values passed to .where using the same prepare logic on column as when inserting the data. I can create a PR for that after this one, if it makes sense to you.

aadamcik avatar Mar 30 '24 13:03 aadamcik

@adamcikado There is a type-checking error in the code. Can you please fix that and then we can release this change.

Sorry it took a while :)

thetutlage avatar May 01 '24 08:05 thetutlage

@thetutlage Thank you and fixed!

Regarding what I mentioned about transforming values passed to .where() in the last comment, what's the best place to discuss it (if it makes sense to you)?

aadamcik avatar May 01 '24 11:05 aadamcik

Regarding what I mentioned about transforming values passed to .where() in the last comment, what's the best place to discuss it (if it makes sense to you)?

I think the simplest option will be to add whereDate, whereDay, whereTime helpers in the query builder and do not perform automatic transformations.

thetutlage avatar May 02 '24 06:05 thetutlage