Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions spp_analytics/models/service_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,10 @@ class AnalyticsCacheEntry(models.Model):
help="When this result was computed",
)

_sql_constraints = [
(
"cache_key_unique",
"UNIQUE(cache_key)",
"Cache key must be unique",
),
]
_cache_key_unique = models.Constraint(
"UNIQUE(cache_key)",
"Cache key must be unique",
)

@api.model
def cron_cleanup_expired(self):
Expand Down
2 changes: 1 addition & 1 deletion spp_metric/models/metric_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MetricCategory(models.Model):
help="Parent category for hierarchical organization",
)

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

@api.constrains("parent_id")
def _check_parent_recursion(self):
Expand Down
2 changes: 1 addition & 1 deletion spp_metric/tests/test_metric_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_category_code_unique(self):
This test verifies the constraint is defined in the model.
"""
# Verify the SQL constraint is defined
constraints = {name for name, _, _ in self.env["spp.metric.category"]._sql_constraints}
constraints = {obj.name for obj in self.env["spp.metric.category"]._table_objects.values()}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Odoo 19, SQL constraints defined via models.Constraint are automatically prefixed with the table name (e.g., spp_metric_category_code_unique). As a result, checking for the bare constraint name "code_unique" on line 34 will fail because obj.name contains the fully prefixed name. Stripping the table prefix from the constraint names in the set comprehension ensures the test passes.

Suggested change
constraints = {obj.name for obj in self.env["spp.metric.category"]._table_objects.values()}
constraints = {obj.name.removeprefix("spp_metric_category_") for obj in self.env["spp.metric.category"]._table_objects.values()}

self.assertIn("code_unique", constraints, "SQL unique constraint should be defined for code field")

def test_category_parent_child(self):
Expand Down
11 changes: 4 additions & 7 deletions spp_scoring/models/scoring_invalid_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ class SppScoringInvalidValue(models.Model):
),
)

_sql_constraints = [
(
"spp_scoring_invalid_value_name_uniq",
"unique(name)",
"An invalid-value entry with this string already exists.",
),
]
_name_uniq = models.Constraint(
"unique(name)",
"An invalid-value entry with this string already exists.",
)

@api.constrains("name", "match_type")
def _check_regex_compiles(self):
Expand Down
Loading