Skip to content

Commit 53c3f0a

Browse files
Merge pull request #215 from OpenSPP/refactor/sql-constraints-to-models-constraint
refactor: migrate _sql_constraints to models.Constraint (Odoo 19)
2 parents f7b7a95 + ca44b5e commit 53c3f0a

4 files changed

Lines changed: 10 additions & 16 deletions

File tree

spp_analytics/models/service_cache.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,10 @@ class AnalyticsCacheEntry(models.Model):
413413
help="When this result was computed",
414414
)
415415

416-
_sql_constraints = [
417-
(
418-
"cache_key_unique",
419-
"UNIQUE(cache_key)",
420-
"Cache key must be unique",
421-
),
422-
]
416+
_cache_key_unique = models.Constraint(
417+
"UNIQUE(cache_key)",
418+
"Cache key must be unique",
419+
)
423420

424421
@api.model
425422
def cron_cleanup_expired(self):

spp_metric/models/metric_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class MetricCategory(models.Model):
5959
help="Parent category for hierarchical organization",
6060
)
6161

62-
_sql_constraints = [("code_unique", "UNIQUE(code)", "Category code must be unique!")]
62+
_code_unique = models.Constraint("UNIQUE(code)", "Category code must be unique!")
6363

6464
@api.constrains("parent_id")
6565
def _check_parent_recursion(self):

spp_metric/tests/test_metric_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_category_code_unique(self):
3030
This test verifies the constraint is defined in the model.
3131
"""
3232
# Verify the SQL constraint is defined
33-
constraints = {name for name, _, _ in self.env["spp.metric.category"]._sql_constraints}
33+
constraints = {obj.name for obj in self.env["spp.metric.category"]._table_objects.values()}
3434
self.assertIn("code_unique", constraints, "SQL unique constraint should be defined for code field")
3535

3636
def test_category_parent_child(self):

spp_scoring/models/scoring_invalid_value.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@ class SppScoringInvalidValue(models.Model):
5757
),
5858
)
5959

60-
_sql_constraints = [
61-
(
62-
"spp_scoring_invalid_value_name_uniq",
63-
"unique(name)",
64-
"An invalid-value entry with this string already exists.",
65-
),
66-
]
60+
_name_uniq = models.Constraint(
61+
"unique(name)",
62+
"An invalid-value entry with this string already exists.",
63+
)
6764

6865
@api.constrains("name", "match_type")
6966
def _check_regex_compiles(self):

0 commit comments

Comments
 (0)