1919 Integer ,
2020 String ,
2121 ForeignKey ,
22- UUID ,
2322 Float ,
2423 Boolean ,
2524 Text ,
2625 DateTime ,
27- func ,
2826)
29- from sqlalchemy .orm import relationship , declared_attr , Mapped , mapped_column
27+ from sqlalchemy .ext .associationproxy import association_proxy
28+ from sqlalchemy .orm import relationship , Mapped , mapped_column
3029from sqlalchemy_utils import TSVectorType
3130
3231from db import Base , AutoBaseMixin
33- from db .lexicon import Lexicon
3432
3533
3634class SampleLocation (Base , AutoBaseMixin ):
@@ -42,21 +40,13 @@ class SampleLocation(Base, AutoBaseMixin):
4240 Geometry (geometry_type = "POINT" , srid = 4326 , spatial_index = True )
4341 )
4442
45- owner_id = Column (Integer , ForeignKey ("owner.id" ), nullable = True )
43+ owner_id = Column (Integer , ForeignKey ("owner.id" , ondelete = 'CASCADE' ), nullable = True )
4644
47-
48- class Asset (Base , AutoBaseMixin ):
49- fs_path = Column (String (255 ), nullable = False , unique = True )
50- name = Column (String (100 ), nullable = False , unique = True )
51- file_type = Column (String (50 ), nullable = False )
52-
53-
54- class AssetLocation (Base , AutoBaseMixin ):
55- asset_id = Column (Integer , ForeignKey ("asset.id" ), nullable = False )
56- location_id = Column (Integer , ForeignKey ("samplelocation.id" ), nullable = False )
57-
58- asset = relationship ("Asset" )
59- location = relationship ("SampleLocation" )
45+ asset_associations = relationship ("AssetLocationAssociation" ,
46+ back_populates = "location" ,
47+ cascade = "all, delete-orphan"
48+ )
49+ assets = association_proxy ("asset_associations" , "asset" )
6050
6151
6252class Owner (Base , AutoBaseMixin ):
@@ -95,7 +85,7 @@ class Contact(Base, AutoBaseMixin):
9585
9686
9787class Well (Base , AutoBaseMixin ):
98- location_id = Column (Integer , ForeignKey ("samplelocation .id" ), nullable = False )
88+ location_id = Column (Integer , ForeignKey ("sample_location .id" , ondelete = "CASCADE " ), nullable = False )
9989
10090 ose_pod_id = Column (String (50 ), nullable = True )
10191 api_id = Column (String (50 ), nullable = True , default = "" ) # API well number
@@ -136,7 +126,7 @@ class Well(Base, AutoBaseMixin):
136126
137127
138128class WellScreen (Base , AutoBaseMixin ):
139- well_id = Column (Integer , ForeignKey ("well.id" ), nullable = False )
129+ well_id = Column (Integer , ForeignKey ("well.id" , ondelete = 'CASCADE' ), nullable = False )
140130 screen_depth_top = Column (
141131 Float , nullable = False , info = {"unit" : "feet below ground surface" }
142132 )
@@ -148,7 +138,7 @@ class WellScreen(Base, AutoBaseMixin):
148138 ) # e.g., "PVC", "Steel", etc.
149139
150140 # Define a relationship to well if needed
151- well = relationship ("Well" )
141+ # well = relationship("Well")
152142
153143
154144class Equipment (Base , AutoBaseMixin ):
@@ -159,14 +149,14 @@ class Equipment(Base, AutoBaseMixin):
159149 date_removed = Column (DateTime )
160150 recording_interval = Column (Integer )
161151 equipment_notes = Column (String (50 ))
162- location_id = Column (Integer , ForeignKey ("samplelocation .id" ), nullable = False )
152+ location_id = Column (Integer , ForeignKey ("sample_location .id" , ondelete = "CASCADE " ), nullable = False )
163153
164154 location = relationship ("SampleLocation" )
165155
166156
167157class Spring (Base , AutoBaseMixin ):
168158 description = Column (String (255 ), nullable = True )
169- location_id = Column (Integer , ForeignKey ("samplelocation .id" ), nullable = False )
159+ location_id = Column (Integer , ForeignKey ("sample_location .id" , ondelete = "CASCADE " ), nullable = False )
170160
171161 # Define a relationship to samplelocations if needed
172162 location = relationship ("SampleLocation" )
@@ -177,12 +167,12 @@ class Group(Base, AutoBaseMixin):
177167 description = Column (String (255 ), nullable = True )
178168
179169 # Define a relationship to samplelocations if needed
180- locations = relationship ("SampleLocation" , secondary = "grouplocation " )
170+ locations = relationship ("SampleLocation" , secondary = "group_location_association " )
181171
182172
183- class GroupLocation (Base , AutoBaseMixin ):
184- group_id = Column (Integer , ForeignKey ("group.id" ), nullable = False )
185- location_id = Column (Integer , ForeignKey ("samplelocation .id" ), nullable = False )
173+ class GroupLocationAssociation (Base , AutoBaseMixin ):
174+ group_id = Column (Integer , ForeignKey ("group.id" , ondelete = 'CASCADE' ), nullable = False )
175+ location_id = Column (Integer , ForeignKey ("sample_location .id" , ondelete = "CASCADE " ), nullable = False )
186176
187177 # group = relationship("Group")
188178 # location = relationship("SampleLocation")
0 commit comments