I'm setting up a PostgreSQL database, and want to create a constraint for checking if values (from different rows in a single table) are consecutive.
The table looks like this:
+-------------+---------+-------+
| waypoint_id | path_id | order |
+-------------+---------+-------+
| 89 | 1 | 1 |
| 16 | 1 | 2 |
| 17 | 1 | 3 |
| 19 | 1 | 4 |
| 4 | 1 | 5 |
| 75 | 2 | 1 |
| 28 | 2 | 2 |
| 2 | 2 | 3 |
+-------------+---------+-------+
It's a table for storing the order of waypoints of a certain path.
- The waypoint_id is a ForeignKey to a table where the coordinates of the waypoints are stored.
- The path_id is a ForeignKey to a table where info about the path is stored (what type of path etc).
- The order is an integer that stores the order of the waypoints in a specific path.
- The PrimaryKey is a composite of all 3 columns.
The constraint has to check if the values in the order column (with equal path_id) are consecutive.
This is an INVALID example: (the order is not consecutive because the 3 is missing)
+-------------+---------+-------+
| waypoint_id | path_id | order |
+-------------+---------+-------+
| 21 | 1 | 1 |
| 29 | 1 | 2 |
| 104 | 1 | 4 |
+-------------+---------+-------+
I expect a constraint to not allow the INSERT of this row:
| 104 | 1 | 4 |
Please show me examples of solutions for similar problems or point me to documentation on how to do his.