How can I prevent certain combinations of values in a table, derived from the values already in the table?

izrik

I'm developing a web app for storing and managing tasks, using Flask-SQLAlchemy to talk to the backend. I want to use a table to store priority comparisons between pairs of tasks in order to construct a partial ordering. The table will have two columns, one for the lesser element and one for the greater element, and both columns together will form the primary key.

My code so far looks like this:

class PriorityPair(db.Model):
    lesser_id = db.Column(db.Integer, db.ForeignKey('task.id'),
                          primary_key=True)
    lesser = db.relationship('Task', remote_side=[id])
    greater_id = db.Column(db.Integer, db.ForeignKey('task.id'),
                           primary_key=True)
    greater = db.relationship('Task', remote_side=[id])

    def __init__(self, lesser, greater):
        self.lesser = lesser
        self.greater = greater

All told, this should be sufficient for what I want to do with the table, but there's a problem in that inconsistent rows might be inserted. Suppose I have two tasks, A and B. If task A is of greater priority than task B, I could do the following:

pair = PriorityPair(task_b, task_a)
db.session.add(pair)
db.session.commit

and the relation between the two would be stored as desired. But if at some future point, the opposite relation, PriorityPair(task_a, task_b), were to be inserted into the table, that would be inconsistent. The two tasks can't be greater in importance than each other at the same time.

Now I could probably prevent this in python code, but I'd like to be sure that the DB table itself is guaranteed to remain consistent. Is it possible to put (via Flask-SqlAlchemy) some kind of constraint on the table, so that if (A,B) is already present, then (B,A) will be automatically rejected? And would such a constraint work across DB backends?

izrik

No and No.

This is not possible. SqlAlchemy has support for CHECK constraints, but the expression of the check is given as a string. It will require a sub-query, something like (greater_id, lesser_id) not in (select sub.lesser_id, sub.greater_id from priority_pair as sub). And the underlying database backends will prevent it:

Instead, you must find some other solution, whether it's triggers or just changing the whole model, which is what I decided to do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I prevent certain combinations of values in a table, derived from the values already in the table?

From Dev

How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

From Dev

How to check if sql values already exist in a table to prevent duplicates?

From Dev

How can I prevent bitwise OR combinations of enum values?

From Dev

How can I get pivot_table to apply a function over combinations of values?

From Dev

How can I create a SQL table from another table without copying any values from the old table

From Dev

How can I replace NaN values in DataFrame from another table?

From Dev

How can I insert values from SELECT SUM into other table?

From Dev

How can I insert values from SELECT SUM into other table?

From Dev

How can I put values from an array into an HTML Table?

From Dev

How can i calculate table values

From Dev

How can I prevent a nested table from enlarging the parent table?

From Dev

How can I prevent a nested table from enlarging the parent table?

From Dev

How can I get and display all values of a certain field in a sql table?

From Dev

How do I get cell values from table rows with a certain class?

From Dev

How can I have access to values in a table with values() or iteritems()

From Dev

How to change a value in a table with already fulfilled values?

From Dev

how to prevent allowing null values in a table

From Dev

In a trigger how to insert values into another table but check if values already exists

From Dev

How can I prevent MySQL numeric values from changing? (phpMyAdmin)

From Dev

How can I prevent values from overlapping in a Python multiprocessing?

From Java

How can I ignore certain returned values from array destructuring?

From Dev

Updating table without replacing values that already exist in the original table with null values from the temporary table

From Dev

I can not get values from table. Relationship one to many

From Dev

How can I SUM values in a joined table without screwing up totals of values in the first table?

From Dev

How can I get value from one table and array of values from another join table in one mysql query?

From Dev

How to get values from table

From Dev

How can I access selected report filter values in Excel Pivot table from VBA code or Formulas?

From Dev

In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

Related Related

  1. 1

    How can I prevent certain combinations of values in a table, derived from the values already in the table?

  2. 2

    How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

  3. 3

    How to check if sql values already exist in a table to prevent duplicates?

  4. 4

    How can I prevent bitwise OR combinations of enum values?

  5. 5

    How can I get pivot_table to apply a function over combinations of values?

  6. 6

    How can I create a SQL table from another table without copying any values from the old table

  7. 7

    How can I replace NaN values in DataFrame from another table?

  8. 8

    How can I insert values from SELECT SUM into other table?

  9. 9

    How can I insert values from SELECT SUM into other table?

  10. 10

    How can I put values from an array into an HTML Table?

  11. 11

    How can i calculate table values

  12. 12

    How can I prevent a nested table from enlarging the parent table?

  13. 13

    How can I prevent a nested table from enlarging the parent table?

  14. 14

    How can I get and display all values of a certain field in a sql table?

  15. 15

    How do I get cell values from table rows with a certain class?

  16. 16

    How can I have access to values in a table with values() or iteritems()

  17. 17

    How to change a value in a table with already fulfilled values?

  18. 18

    how to prevent allowing null values in a table

  19. 19

    In a trigger how to insert values into another table but check if values already exists

  20. 20

    How can I prevent MySQL numeric values from changing? (phpMyAdmin)

  21. 21

    How can I prevent values from overlapping in a Python multiprocessing?

  22. 22

    How can I ignore certain returned values from array destructuring?

  23. 23

    Updating table without replacing values that already exist in the original table with null values from the temporary table

  24. 24

    I can not get values from table. Relationship one to many

  25. 25

    How can I SUM values in a joined table without screwing up totals of values in the first table?

  26. 26

    How can I get value from one table and array of values from another join table in one mysql query?

  27. 27

    How to get values from table

  28. 28

    How can I access selected report filter values in Excel Pivot table from VBA code or Formulas?

  29. 29

    In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

HotTag

Archive