Hello,
I faced with an issue when the order of nested assocs during insert matters. It matters because I have custom check constraint which excludes rows by complex rules. What are the options? I think only the work around here is to use a transaction and make each insert by hand?
Thanks.
UPDATE
I think that I am ignored because it could not be clear. Sorry about that. So I decided to add extra cases to show my problem.
Let’s assume I have the next table
CREATE TABLE date_ranges (
id SERIAL PRIMARY KEY,
user_id INTEGER,
start_at DATE,
end_at DATE,
is_exception BOOLEAN
is_day_off BOOLEAN
);
The use schema has next fields:
has_many :date_ranges, DateRange, on_replace: :delete
has_many :except_ranges, DateRange, on_replace: :delete
has_many :day_off_ranges, DateRange, on_replace: :delete
When I do my insert the order of date_ranges
, except_ranges
, day_off_ranges
is not frozen. The log shows that except_ranges
insert before date_ranges
. And I would like to specify the priority to insert date_ranges
first and then the rest.
I have PGSQL functions to check every case to exclude ranges according to the table:
user_id start_at end_at is_exception is_day_off
1 2019-03-01 2019-03-05 FALSE FALSE // is fine
1 2019-03-01 2019-03-05 FALSE FALSE // FAIL. Range intersects with range above
1 2019-03-02 2019-03-03 TRUE FALSE // is fine since exception is inside "regular range above"
1 2019-03-04 2019-03-04 TRUE FALSE // is fine since exception is inside "regular range above" & does not intersect another exception
1 2019-03-03 2019-03-03 FALSE TRUE // is fine since day off is inside "regular range above"
1 2019-03-05 2019-03-05 FALSE TRUE // is fine since day off is inside "regular range above" & does not intersect another day off
1 2019-03-02 2019-03-03 TRUE FALSE // FAIL. Exception should NOT intersect with another one
1 2019-03-03 2019-03-03 FALSE TRUE // FAIL. Day off should NOT intersect with another one
1 2019-03-15 2019-03-15 TRUE FALSE // FAIL. Exception is NOT inside "regular range above"
1 2019-03-10 2019-03-10 FALSE TRUE // FAIL. Day off is NOT inside "regular range above"
At the end I get {:error, changeset}
because excepted_ranges
is inserted first.