solidus icon indicating copy to clipboard operation
solidus copied to clipboard

Make option value to variant association unique

Open jarednorman opened this issue 2 years ago • 2 comments

Description

I can't think of a reason why someone would want to associate the same option value with a given variant multiple times. This makes the index on the variant/option_value_id columns of the spree_option_values_variants join table a unique index so we can't have a variant that has the same option value multiple times.

Checklist:

  • [x] I have followed Pull Request guidelines
  • [x] I have added a detailed description into each commit message
  • [ ] I have updated Guides and README accordingly to this change (if needed)
  • [x] I have added tests to cover this change (if needed)
  • [ ] I have attached screenshots to this PR for visual changes (if needed)

jarednorman avatar Aug 15 '21 01:08 jarednorman

I don't think any of the errors have anything to do with my change. 🤔

jarednorman avatar Aug 16 '21 20:08 jarednorman

Had to scroll back more. I used 6.2 as the migration version, but currently we have to write migrations for the lowest supported ActiveRecord version.

jarednorman avatar Aug 16 '21 23:08 jarednorman

This is not to hold this back: We associate the same option value with multiple variants. We have a third field, an integer, that differentiates the variants from one another. We are a special case though as we sell a lot of stuff in differently sized bags.

mamhoff avatar Jan 31 '23 09:01 mamhoff

@mamhoff I don't think this will impact you then? You can still have multiple variants per option value, just not the the same option value on one variant multiple times.

jarednorman avatar Jan 31 '23 16:01 jarednorman

Ah true! I mistakenly thought we need to have different option value combos for each variant. :+1:

mamhoff avatar Jan 31 '23 16:01 mamhoff

Hey @jarednorman, can you please rebase this one? Thanks! 🙏

kennyadsl avatar Apr 24 '23 09:04 kennyadsl

Hey @jarednorman, can you please rebase this one? Thanks! pray

I did it myself and fixed the conflict

waiting-for-dev avatar Apr 25 '23 13:04 waiting-for-dev

This would be a good chance to mark the variant_id and the option_value_id as NOT NULL and add the two foreign key constraints. I would assume ON DELETE CASCADE ON UPDATE CASCADE would be appropriate here.

BenMorganIO avatar Apr 25 '23 19:04 BenMorganIO

This would be a good chance to mark the variant_id and the option_value_id as NOT NULL and add the two foreign key constraints. I would assume ON DELETE CASCADE ON UPDATE CASCADE would be appropriate here.

We decided to skip this kind of changes for this major release. There are already a bunch of database changes and we don't want to be a nightmare to update.

@BenMorganIO thanks a lot for the suggestion, I think we should design a process to make these changes safer. An idea is a script that runs at boot and checks the database (especially important in production) and gives some hints if you need to fix inconsistencies. BTW, we can discuss in Slack or in a separate issue/PR if you want.

kennyadsl avatar Apr 26 '23 14:04 kennyadsl