Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

atombrella
Copy link
Contributor

@atombrella atombrella commented Oct 4, 2018

It should be cleaned up, but here's a good first take on adding the exclude using constraint to django.contrib.postgres. Essentially, it's a way to enforce a unique constraint for a range, whether they're represented as a range-type, or using two fields.

https://gist.github.com/fphilipe/0a2a3d50a9f3834683bf uses two fields, and overlaps with the documentation example where cars are used to illustrate how it works.

I was lucky that RemoveConstraint works, as the constraint can also be removed using DROP INDEX ....
https://code.djangoproject.com/ticket/29824

@atombrella atombrella force-pushed the exclude_using branch 2 times, most recently from fac7ab0 to 8c2709e Compare October 4, 2018 10:37
@atombrella atombrella changed the title Exclude using Fixed #29824 -- Added exclude using constraint. Oct 4, 2018
Copy link
Member

@charettes charettes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work on this!

@timgraham
Copy link
Member

Is it ready for review or still WIP as mentioned in the commit message?

@atombrella
Copy link
Contributor Author

@timgraham I'd like to have an example as requested because there may be many (I've at least worked with one) existing installations of Django which implement a table with two fields, rather than using django.contrib.postgres's ranges. I tried to add some test and code, just as a POC but didn't finalize it.

Copy link
Contributor

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and please rebase :)

@atombrella atombrella force-pushed the exclude_using branch 2 times, most recently from 57d5855 to 8a2f31f Compare May 24, 2019 15:02
@atombrella
Copy link
Contributor Author

I'm clueless how to fix the failing tests on the databases that are irrelevant for this patch.

Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @atombrella!

Thanks for tackling this - will be an awesome addition. I've had a first pass through this - I think that there is some validation that needs attending to and I have some questions about simplifying the API...

@atombrella atombrella force-pushed the exclude_using branch 2 times, most recently from f251951 to a187867 Compare May 26, 2019 20:35
@atombrella
Copy link
Contributor Author

@ngnpope Thank you 👍 I think I've addressed most of the feedback that was possible. Remaining issues are related to && versus constants such as OVERLAPS. I like the idea of F().with() and Expression().with(), but that'd introduce a subclass of F and Expression somewhere in the PostgreSQL backend just for this purpose. About documentation, referring to PostgreSQL's documentation for details on the operators will suffice. People who will want to use this feature also have a good understanding of PostgreSQL.

Tests are failing on unrelated database backends, and suggested method of silencing with try/except pass doesn't work. Help will be appreciated.

The docs part of the test fails because of customise not being recognized. @carltongibson or @felixxm Could you please fix this?

@felixxm
Copy link
Member

felixxm commented Jun 3, 2019

@atombrella #11439.

Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to have a think about your questions - hopefully I'll have some time later this week.

@atombrella atombrella force-pushed the exclude_using branch 2 times, most recently from 733b682 to a2c42ab Compare June 14, 2019 09:45
@atombrella atombrella force-pushed the exclude_using branch 4 times, most recently from 5291113 to facc286 Compare July 1, 2019 12:34
@carltongibson carltongibson self-requested a review July 1, 2019 12:49
@felixxm
Copy link
Member

felixxm commented Jul 13, 2019

@atombrella I've prepared first commit (with RangeOperators) ☺️ .

Quick comments:

  • We should add test (with subTest) to check all operators at least once (I found and fixed a typo in RangeOperators.CONTAINED_BY).

  • Do we need RangeOperators.UNION, RangeOperators.INTERSECTION and RangeOperators.DIFFERENCE? I don't think that they are useful for exclusion constraints.

I'm going to review in details remaining docs and tests in the next week.

@atombrella
Copy link
Contributor Author

atombrella commented Jul 13, 2019

@felixxm Yes, some of those operators may not be relevant. But I don't think it'll hurt to add them. I'll try to think of a way to test them.

I think @ngnpope gave the documentation a pretty good first review, so I don't think a review of this part will be tricky :) The tests have not yet really been looked at.

@felixxm
Copy link
Member

felixxm commented Jul 13, 2019

@atombrella I think that only RangeOperators.UNION, RangeOperators.INTERSECTION and RangeOperators.DIFFERENCE are not useful for exclusion constraints. We should avoid adding unrelated code. Please remove them from this PR, we may decide to add Union(), Intersection() and Difference() functions for ranges in the future, but they aren't in scope of this PR.

Thanks for understanding.

@atombrella
Copy link
Contributor Author

atombrella commented Jul 14, 2019

@felixxm I removed those three operators. The easiest way I could think that fulfills this was using the examples in PostgreSQL documentation and cursor.execute('select ...'). Thanks for the help :)

@felixxm felixxm force-pushed the exclude_using branch 4 times, most recently from e318ec5 to aa172ef Compare July 15, 2019 18:21
@felixxm
Copy link
Member

felixxm commented Jul 15, 2019

@atombrella I made some changes:

  • added create_sql() and remove_sql() to ExclusionConstraint,
  • changed docs and signature of RangeBoundary (I think it is simpler without constants),
  • made edits in docs/ref/contrib/postgres/fields.txt ,
  • added RangeBoundary to the release notes,
  • simplified tests.

My plan for tomorrow is to review remaining docs and add tests for exclusion constraints with other operators.

@atombrella
Copy link
Contributor Author

👍 to RangeBoundary changes. It reads a lot nicer! I'm not sure why create_sql and remove_sql are needed, but I'm indifferent about them. I see many good simplifications :) Thanks!

@felixxm
Copy link
Member

felixxm commented Jul 16, 2019

@atombrella Only commutative operators can be used in exclusion constraints, so I'm going to remove currently unused LT, GT, LTE, and GTE.

@felixxm
Copy link
Member

felixxm commented Jul 16, 2019

@atombrella Without create_sql() and remove_sql() you will not be able to add/remove constraints, only defining constraints for a new model will work.

@felixxm
Copy link
Member

felixxm commented Jul 16, 2019

I added test for ADJACENT_TO operator and validation for expressions.

@atombrella
Copy link
Contributor Author

@felixxm OK. Maybe you should add Co-Authored-By in the commit message? You've done a fair bit of the work here. I'm learning and appreciate the help.

@felixxm felixxm force-pushed the exclude_using branch 2 times, most recently from aa0d8df to 2d7c6fb Compare July 16, 2019 11:19
@felixxm
Copy link
Member

felixxm commented Jul 16, 2019

@atombrella You're a 🌟 ! I refactored docs and moved exclusion constraints to the main section instead of minor features, It's not minor 🚀 . Now I'm happy 😉

@felixxm felixxm changed the title Fixed #29824 -- Added exclude using constraint. Fixed #29824 -- Added support for database exclusion constraints on PostgreSQL. Jul 16, 2019
atombrella and others added 2 commits July 16, 2019 16:57
…ostgreSQL.

Thanks to Nick Pope and Mariusz Felisiak for review.

Co-Authored-By: Mariusz Felisiak <[email protected]>
@felixxm felixxm merged commit a341728 into django:master Jul 16, 2019
@felixxm
Copy link
Member

felixxm commented Jul 16, 2019

@atombrella Many thanks 🚀 🎊 Thanks y'all for comments and reviews 🙇‍♂️

@atombrella atombrella deleted the exclude_using branch July 16, 2019 19:29
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.

7 participants