Skip to content

Commit 6c58290

Browse files
authored
fix: use correct constraint on Observations (#15235)
1 parent 30c8720 commit 6c58290

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
"""
13+
cascade observation delete
14+
15+
Revision ID: 812e14a4cddf
16+
Revises: a073e7979805
17+
Create Date: 2024-01-18 18:46:58.482270
18+
"""
19+
20+
from alembic import op
21+
22+
revision = "812e14a4cddf"
23+
down_revision = "9a0ed2044b53"
24+
25+
26+
def upgrade():
27+
op.drop_constraint(
28+
"project_observations_related_id_fkey",
29+
"project_observations",
30+
type_="foreignkey",
31+
)
32+
op.create_foreign_key(
33+
None,
34+
"project_observations",
35+
"projects",
36+
["related_id"],
37+
["id"],
38+
onupdate="CASCADE",
39+
ondelete="CASCADE",
40+
)
41+
op.drop_constraint(
42+
"release_observations_related_id_fkey",
43+
"release_observations",
44+
type_="foreignkey",
45+
)
46+
op.create_foreign_key(
47+
None,
48+
"release_observations",
49+
"releases",
50+
["related_id"],
51+
["id"],
52+
onupdate="CASCADE",
53+
ondelete="CASCADE",
54+
)
55+
56+
57+
def downgrade():
58+
op.drop_constraint(
59+
"release_observations_related_id_fkey",
60+
"release_observations",
61+
type_="foreignkey",
62+
)
63+
op.create_foreign_key(
64+
"release_observations_related_id_fkey",
65+
"release_observations",
66+
"releases",
67+
["related_id"],
68+
["id"],
69+
)
70+
op.drop_constraint(
71+
"project_observations_related_id_fkey",
72+
"project_observations",
73+
type_="foreignkey",
74+
)
75+
op.create_foreign_key(
76+
"project_observations_related_id_fkey",
77+
"project_observations",
78+
"projects",
79+
["related_id"],
80+
["id"],
81+
)

warehouse/observations/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ def observations(cls): # noqa: N805
184184
},
185185
related_id=mapped_column(
186186
PG_UUID,
187-
ForeignKey(f"{cls.__tablename__}.id"),
187+
ForeignKey(
188+
f"{cls.__tablename__}.id",
189+
onupdate="CASCADE",
190+
ondelete="CASCADE",
191+
),
188192
comment="The ID of the related model",
189193
nullable=False,
190194
index=True,
@@ -199,7 +203,7 @@ def observations(cls): # noqa: N805
199203
observer=relationship(Observer),
200204
),
201205
)
202-
return relationship(cls.Observation)
206+
return relationship(cls.Observation, cascade="all, delete-orphan")
203207

204208
def record_observation(
205209
self,

0 commit comments

Comments
 (0)