diff --git a/extension/toar_controlled_vocabulary-0.1/toar_controlled_vocabulary--0.1.sql b/extension/toar_controlled_vocabulary-0.1/toar_controlled_vocabulary--0.1.sql index 52bfa7fa84448100f81e13c3b5688d40b9453d18..0a57fd627b58aabe2c73e50cc4fef433306a6485 100644 --- a/extension/toar_controlled_vocabulary-0.1/toar_controlled_vocabulary--0.1.sql +++ b/extension/toar_controlled_vocabulary-0.1/toar_controlled_vocabulary--0.1.sql @@ -20,7 +20,8 @@ CREATE TABLE IF NOT EXISTS RC_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT rc_enum_val_unique UNIQUE (enum_val) ); INSERT INTO RC_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -37,7 +38,8 @@ CREATE TABLE IF NOT EXISTS RS_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT rs_enum_val_unique UNIQUE (enum_val) ); INSERT INTO RS_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -54,7 +56,8 @@ CREATE TABLE IF NOT EXISTS OK_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT ok_enum_val_unique UNIQUE (enum_val) ); INSERT INTO OK_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -75,7 +78,8 @@ CREATE TABLE IF NOT EXISTS DA_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT da_enum_val_unique UNIQUE (enum_val) ); INSERT INTO DA_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -89,7 +93,8 @@ CREATE TABLE IF NOT EXISTS SF_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT sf_enum_val_unique UNIQUE (enum_val) ); INSERT INTO SF_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -109,7 +114,8 @@ CREATE TABLE IF NOT EXISTS AT_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT at_enum_val_unique UNIQUE (enum_val) ); INSERT INTO AT_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -127,7 +133,8 @@ CREATE TABLE IF NOT EXISTS DS_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT ds_enum_val_unique UNIQUE (enum_val) ); INSERT INTO DS_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -140,7 +147,8 @@ CREATE TABLE IF NOT EXISTS MM_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT mm_enum_val_unique UNIQUE (enum_val) ); INSERT INTO MM_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -157,7 +165,8 @@ CREATE TABLE IF NOT EXISTS CZ_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT cz_enum_val_unique UNIQUE (enum_val) ); INSERT INTO CZ_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -182,7 +191,8 @@ CREATE TABLE IF NOT EXISTS CV_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT cv_enum_val_unique UNIQUE (enum_val) ); INSERT INTO CV_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -198,7 +208,8 @@ CREATE TABLE IF NOT EXISTS ST_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT st_enum_val_unique UNIQUE (enum_val) ); INSERT INTO ST_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -214,7 +225,8 @@ CREATE TABLE IF NOT EXISTS TA_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT ta_enum_val_unique UNIQUE (enum_val) ); INSERT INTO TA_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -230,14 +242,16 @@ CREATE TABLE IF NOT EXISTS TC_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT tc_enum_val_unique UNIQUE (enum_val) ); INSERT INTO TC_vocabulary (enum_val, enum_str, enum_display_str) VALUES - (0, 'Unclassified', 'unclassified'), - (1, 'RuralLowElevation', 'rural low elevation'), - (2, 'RuralHighElevation', 'rural high elevation'), - (3, 'Urban', 'urban'); + (-1, 'Unknown', 'unknown'), + ( 0, 'Unclassified', 'unclassified'), + ( 1, 'RuralLowElevation', 'rural low elevation'), + ( 2, 'RuralHighElevation', 'rural high elevation'), + ( 3, 'Urban', 'urban'); -- Station HTAP Regions (TIER1) -- The integer denoting the “tier1” region defined in the task force on hemispheric transport of air pollution (TFHTAP) coordinated model studies. @@ -246,7 +260,8 @@ CREATE TABLE IF NOT EXISTS TR_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT tr_enum_val_unique UNIQUE (enum_val) ); INSERT INTO TR_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -276,7 +291,8 @@ CREATE TABLE IF NOT EXISTS DL_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT dl_enum_val_unique UNIQUE (enum_val) ); INSERT INTO DL_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -306,7 +322,8 @@ CREATE TABLE IF NOT EXISTS RT_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT rt_enum_val_unique UNIQUE (enum_val) ); INSERT INTO RT_vocabulary (enum_val, enum_str, enum_display_str) VALUES @@ -323,7 +340,8 @@ CREATE TABLE IF NOT EXISTS DF_vocabulary ( enum_val INT NOT NULL, enum_str character varying(128) NOT NULL, enum_display_str character varying(128) NOT NULL, - PRIMARY KEY(enum_val, enum_str) + PRIMARY KEY(enum_val, enum_str), + CONSTRAINT df_enum_val_unique UNIQUE (enum_val) ); INSERT INTO DF_vocabulary (enum_val, enum_str, enum_display_str) VALUES diff --git a/toardb/contacts/models_organisation.py b/toardb/contacts/models_organisation.py index 3befad731e35acd1bd3334a6668211ca1795595b..5370372c219c658ad1e1a759b39a91a6586c8767 100644 --- a/toardb/contacts/models_organisation.py +++ b/toardb/contacts/models_organisation.py @@ -35,6 +35,8 @@ class Organisation(Base): "organisations_pkey" PRIMARY KEY, btree (id) Check constraints: "organisations_kind_check" CHECK (kind >= 0) + Foreign-key constraints: + "organisations_kind_fk_ok_vocabulary_enum_val" FOREIGN KEY (kind) REFERENCES ok_vocabulary(enum_val) """ __tablename__ = 'organisations' __table_args__ = ( @@ -44,10 +46,11 @@ class Organisation(Base): id = Column(Integer, ORGANISATIONS_ID_SEQ, primary_key=True, server_default=ORGANISATIONS_ID_SEQ.next_value()) name = Column(String(32), nullable=False) longname = Column(String(128), nullable=False) - kind = Column(Integer, nullable=False) + kind = Column(ForeignKey('ok_vocabulary.enum_val'), nullable=False) city = Column(String(64), nullable=False) postcode = Column(String(16), nullable=False) street_address = Column(String(128), nullable=False) country = Column(String(64), nullable=False) homepage = Column(String(200), nullable=False) +# ok_vocabulary = relationship('OkVocabulary') diff --git a/toardb/data/models.py b/toardb/data/models.py index 9c82e76bb9711db3d15e4694a7c893ff3a3a81fe..db86f7d0e00eb1aa5f52e9da79b25f8b4e50dc7d 100644 --- a/toardb/data/models.py +++ b/toardb/data/models.py @@ -31,8 +31,9 @@ class Data(Base): "data_value_idx" btree (value) Check constraints: "data_flags_check" CHECK (flags >= 0) - Foreign-key constraints: - "data_timeseries_id_fkey" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) + Foreign-key constraints: + "data_flags_fk_df_vocabulary_enum_val" FOREIGN KEY (flags) REFERENCES df_vocabulary(enum_val) + "data_timeseries_id_a38c5a1a_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED """ __tablename__ = 'data' @@ -42,7 +43,7 @@ class Data(Base): datetime = Column(DateTime(True), nullable=False, index=True) value = Column(Float(53), nullable=False, index=True) - flags = Column(Integer, nullable=False) + flags = Column(ForeignKey('df_vocabulary.enum_val'), nullable=False) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U diff --git a/toardb/stationmeta/models_core.py b/toardb/stationmeta/models_core.py index f63cd95f9e7422e2d396e09d654e8b82eb95c386..074af609c9e48e9f31da8fab1e3f8e9721fd278e 100644 --- a/toardb/stationmeta/models_core.py +++ b/toardb/stationmeta/models_core.py @@ -61,6 +61,9 @@ class StationmetaCore(Base): "stationmeta_core_type_of_environment_check" CHECK (type_of_environment >= 0) Foreign-key constraints: "stationmeta_core_coordinate_validator_38c0ef8d_fk_auth_user" FOREIGN KEY (coordinate_validator_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED + "stationmeta_core_coord_valid_status_fk_cv_vocabulary_enum_val" FOREIGN KEY (coordinate_validation_status) REFERENCES cv_vocabulary(enum_val) + "stationmeta_core_type_of_area_fk_ta_vocabulary_enum_val" FOREIGN KEY (type_of_area) REFERENCES ta_vocabulary(enum_val) + "stationmeta_core_type_of_environment_fk_st_vocabulary_enum_val" FOREIGN KEY (type_of_environment) REFERENCES st_vocabulary(enum_val) Referenced by: TABLE "station_roles" CONSTRAINT "station_roles_station_id_f31f80fc_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED TABLE "stationmeta_annotations" CONSTRAINT "stationmeta_annotati_station_id_9d3fe3d0_fk_stationme" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED @@ -84,10 +87,10 @@ class StationmetaCore(Base): coordinates = Column(Geometry('POINTZ', 4326)) country = Column(String(128), nullable=False, index=True) state = Column(String(128), nullable=False, index=True) - coordinate_validation_status = Column(Integer, nullable=False) + coordinate_validation_status = Column(ForeignKey('cv_vocabulary.enum_val'), nullable=False) coordinate_validation_date = Column(DateTime(True), nullable=False) - type_of_environment = Column(Integer, nullable=False) - type_of_area = Column(Integer, nullable=False) + type_of_environment = Column(ForeignKey('st_vocabulary.enum_val'), nullable=False) + type_of_area = Column(ForeignKey('ta_vocabulary.enum_val'), nullable=False) category = Column(String(128), nullable=False) timezone = Column(String(64), nullable=False) additional_metadata = Column(JSONB(astext_type=Text()), nullable=True) @@ -101,3 +104,6 @@ class StationmetaCore(Base): globalmeta = relationship('StationmetaGlobal', uselist=False, back_populates='station') globalservice = relationship('StationmetaGlobalService', uselist=False, back_populates='station') +# cv_vocabulary = relationship('CvVocabulary') +# ta_vocabulary = relationship('TaVocabulary') +# st_vocabulary = relationship('StVocabulary') diff --git a/toardb/stationmeta/models_global.py b/toardb/stationmeta/models_global.py index 9347d32e2b8db87e9c827ae9c7043d11687ba5ef..a740374c0c907b9b8ec70799cdf4e8c63c2c9f6f 100644 --- a/toardb/stationmeta/models_global.py +++ b/toardb/stationmeta/models_global.py @@ -64,6 +64,10 @@ class StationmetaGlobal(Base): "stationmeta_global_toar1_category_check" CHECK (toar1_category >= 0) Foreign-key constraints: "stationmeta_global_station_id_29ff53dd_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED + "stationmeta_glob_dominant_landcover_year2012_fk_dl_voc_enum_val" FOREIGN KEY (dominant_landcover_year2012) REFERENCES dl_vocabulary(enum_val) + "stationmeta_global_climatic_zone_fk_cz_at_vocabulary_enum_val" FOREIGN KEY (climatic_zone) REFERENCES cz_vocabulary(enum_val) + "stationmeta_global_htap_region_tier1_fk_tr_vocabulary_enum_val" FOREIGN KEY (htap_region_tier1) REFERENCES tr_vocabulary(enum_val) + "stationmeta_global_toar1_category_fk_tc_vocabulary_enum_val" FOREIGN KEY (toar1_category) REFERENCES tc_vocabulary(enum_val) """ __tablename__ = 'stationmeta_global' # Default values do not fit CheckConstraints == > Check, how both can be done!! @@ -77,7 +81,7 @@ class StationmetaGlobal(Base): id = Column(Integer, STATIONMETA_GLOBAL_ID_SEQ, primary_key=True, server_default=STATIONMETA_GLOBAL_ID_SEQ.next_value()) population_density_year2010 = Column(Float(53), nullable=False, server_default=text("'-1.0'::numeric")) max_population_density_25km_year2010 = Column(Float(53), nullable=False, server_default=text("'-1.0'::numeric")) - climatic_zone = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + climatic_zone = Column(ForeignKey('cz_vocabulary.enum_val'), nullable=False, server_default=text("'-1'::integer")) nightlight_1km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) nightlight_5km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) max_nightlight_25km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) @@ -85,12 +89,12 @@ class StationmetaGlobal(Base): rice_production_year2000 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) edgar_htap_v2_nox_emissions_year2010 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) omi_no2_column_years2011to2015 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) - htap_region_tier1 = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + htap_region_tier1 = Column(ForeignKey('tr_vocabulary.enum_val'), nullable=False, server_default=text("'-1'::integer")) etopo_alt = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) etopo_min_alt_5km = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) etopo_relative_alt = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) - dominant_landcover_year2012 = Column(Integer, nullable=False, server_default=text("'-1'::integer")) - toar1_category = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + dominant_landcover_year2012 = Column(ForeignKey('dl_vocabulary.enum_val'), nullable=False, server_default=text("'-1'::integer")) + toar1_category = Column(ForeignKey('tc_vocabulary.enum_val'), nullable=False, server_default=text("'-1'::integer")) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U @@ -100,3 +104,9 @@ class StationmetaGlobal(Base): #uselist=False ==> 1:1 relationship # station = relationship('StationmetaCore', uselist=False) station = relationship('StationmetaCore', back_populates='globalmeta') + +# cz_vocabulary = relationship('CzVocabulary') +# dl_vocabulary = relationship('DlVocabulary') +# tr_vocabulary = relationship('TrVocabulary') +# tc_vocabulary = relationship('TcVocabulary') + diff --git a/toardb/stationmeta/models_global_services.py b/toardb/stationmeta/models_global_services.py index 8db8f4c190b97186ecde1df7c1387c5e750ba5de..7813a4f82a7dd52a4cbe22e2352d2aaa2cf1baa7 100644 --- a/toardb/stationmeta/models_global_services.py +++ b/toardb/stationmeta/models_global_services.py @@ -35,6 +35,9 @@ class StationmetaGlobalService(Base): "stationmeta_global_services_result_nvalues_check" CHECK (result_nvalues >= 0) "stationmeta_global_services_result_type_check" CHECK (result_type >= 0) "stationmeta_global_services_service_valid_year_check" CHECK (service_valid_year >= 0) + Foreign-key constraints: + "stationmeta_global_services_result_type_fk_rt_voc_enum_val" FOREIGN KEY (result_type) REFERENCES rt_vocabulary(enum_val) + "stationmeta_global_services_station_id_fkey" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) """ __tablename__ = 'stationmeta_global_services' @@ -46,10 +49,12 @@ class StationmetaGlobalService(Base): id = Column(Integer, STATIONMETA_GLOBAL_SERVICES_ID_SEQ, primary_key=True, server_default=STATIONMETA_GLOBAL_SERVICES_ID_SEQ.next_value()) variable_name = Column(String(64), nullable=False) - result_type = Column(Integer, nullable=False) + result_type = Column(ForeignKey('rt_vocabulary.enum_val'), nullable=False) result_nvalues = Column(Integer, nullable=False) service_valid_year = Column(Integer) service_url = Column(String(200), nullable=False, unique=True) station_id = Column(ForeignKey(StationmetaCore.id)) station = relationship('StationmetaCore', back_populates='globalservice') +# rt_vocabulary = relationship('RtVocabulary') + diff --git a/toardb/stationmeta/models_role.py b/toardb/stationmeta/models_role.py index c49f9b3ae17175733032942bd0e50593106cba5b..e05259178debc66bfe84e8fa6ba625b13f2c6441 100644 --- a/toardb/stationmeta/models_role.py +++ b/toardb/stationmeta/models_role.py @@ -40,6 +40,8 @@ class StationmetaRole(Base): "stationmeta_roles_status_check" CHECK (status >= 0) Foreign-key constraints: "stationmeta_roles_person_id_3bd9c160_fk_persons_id" FOREIGN KEY (person_id) REFERENCES persons(id) DEFERRABLE INITIALLY DEFERRED + "stationmeta_roles_role_fk_rc_vocabulary_enum_val" FOREIGN KEY (role) REFERENCES rc_vocabulary(enum_val) + "stationmeta_roles_status_fk_rs_vocabulary_enum_val" FOREIGN KEY (status) REFERENCES rs_vocabulary(enum_val) """ __tablename__ = 'stationmeta_roles' @@ -49,8 +51,8 @@ class StationmetaRole(Base): ) id = Column(Integer, STATIONMETA_ROLES_ID_SEQ, primary_key=True, server_default=STATIONMETA_ROLES_ID_SEQ.next_value()) - role = Column(Integer, nullable=False) - status = Column(Integer, nullable=False) + role = Column(ForeignKey('rc_vocabulary.enum_val'), nullable=False) + status = Column(ForeignKey('rs_vocabulary.enum_val'), nullable=False) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U @@ -62,3 +64,5 @@ class StationmetaRole(Base): secondary=stationmeta_core_stationmeta_roles_table, backref="roles") +# rc_vocabulary = relationship('RcVocabulary') +# rs_vocabulary = relationship('RsVocabulary') diff --git a/toardb/timeseries/models.py b/toardb/timeseries/models.py index daa5c3832d8517687eb7863a2c28786057ded07a..352acbbd1dfd9e879e734804614ddf0420493fc7 100644 --- a/toardb/timeseries/models.py +++ b/toardb/timeseries/models.py @@ -77,3 +77,16 @@ DS_enum = ( Enumdict(1, 'Measurement', 'measurement') ) +# Measurement Methods +MM_enum_table = Table("mm_vocabulary", + Base.metadata, + Column("enum_val", Integer, primary_key=True), + Column("enum_str", String), + Column("enum_display_str", String) + ) +# The following code is just a workaround (see stationmeta/models.py): +MM_enum = ( + Enumdict(0, 'UVAbsorption', 'UV absorption'), + Enumdict(1, 'UnknownInstrument', 'unknown instrument') + ) + diff --git a/toardb/timeseries/models_core.py b/toardb/timeseries/models_core.py index 4ad8e1243ac9f971a3c636c98abe38b6ee29d6b5..3492dd071a59dee5b52141c2b2aebcb771d5c635 100644 --- a/toardb/timeseries/models_core.py +++ b/toardb/timeseries/models_core.py @@ -37,7 +37,7 @@ class Timeseries(Base): +---------------------+--------------------------+-----------+----------+----------------------------------------+ | data_end_date | timestamp with time zone | | not null | | +---------------------+--------------------------+-----------+----------+----------------------------------------+ - | measurement_method | character varying(128) | | not null | | + | measurement_method | integer | | not null | 1 | +---------------------+--------------------------+-----------+----------+----------------------------------------+ | sampling_height | double precision | | not null | | +---------------------+--------------------------+-----------+----------+----------------------------------------+ @@ -63,7 +63,12 @@ class Timeseries(Base): "timeseries_sampling_frequency_check" CHECK (sampling_frequency >= 0) Foreign-key constraints: "timeseries_station_id_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED - "timeseries_variable_id_fk_variables_id" FOREIGN KEY (variable_id) REFERENCES variables(id) DEFERRABLE INITIALLY DEFERRED + "timeseries_variable_id_fk_variables_id" FOREIGN KEY (variable_id) REFERENCES variables(id) DEFERRABLE INITIALLY DEFERREDForeign-key constraints: + "timeseries_access_rights_fk_da_vocabulary_enum_val" FOREIGN KEY (access_rights) REFERENCES da_vocabulary(enum_val) + "timeseries_aggregation_fk_at_vocabulary_enum_val" FOREIGN KEY (aggregation) REFERENCES at_vocabulary(enum_val) + "timeseries_measurement_method_fk_mm_vocabulary_enum_val" FOREIGN KEY (measurement_method) REFERENCES mm_vocabulary(enum_val) + "timeseries_sampling_frequency_fk_sf_vocabulary_enum_val" FOREIGN KEY (sampling_frequency) REFERENCES sf_vocabulary(enum_val) + "timeseries_source_fk_ds_vocabulary_enum_val" FOREIGN KEY (source) REFERENCES ds_vocabulary(enum_val) Referenced by: TABLE "data" CONSTRAINT "data_timeseries_id_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED TABLE "timeseries_timeseries_annotations" CONSTRAINT "timeseries_timeseries_annotations_timeseries_id_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED @@ -83,13 +88,13 @@ class Timeseries(Base): id = Column(Integer, TIMESERIES_ID_SEQ, primary_key=True, server_default=TIMESERIES_ID_SEQ.next_value()) label = Column(String(128), nullable=False) order = Column(Integer, nullable=False) - access_rights = Column(Integer, nullable=False) - sampling_frequency = Column(Integer, nullable=False) - aggregation = Column(Integer, nullable=False) - source = Column(Integer, nullable=False, server_default=text("1")) + access_rights = Column(ForeignKey('da_vocabulary.enum_val'), nullable=False) + sampling_frequency = Column(ForeignKey('sf_vocabulary.enum_val'), nullable=False) + aggregation = Column(ForeignKey('at_vocabulary.enum_val'), nullable=False) + source = Column(ForeignKey('ds_vocabulary.enum_val'), nullable=False, server_default=text("1")) data_start_date = Column(DateTime(True), nullable=False) data_end_date = Column(DateTime(True), nullable=False) - measurement_method = Column(String(128), nullable=False) + measurement_method = Column(ForeignKey('mm_vocabulary.enum_val'), nullable=False, server_default=text("1")) sampling_height = Column(Float(53), nullable=False) additional_metadata = Column(JSONB(astext_type=Text()), nullable=True) date_added = Column(DateTime(True), nullable=False, server_default=text("now()")) @@ -99,7 +104,12 @@ class Timeseries(Base): # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U station_id = Column(ForeignKey(StationmetaCore.id, deferrable=True, initially='DEFERRED'), index=True) variable_id = Column(ForeignKey(Variable.id, deferrable=True, initially='DEFERRED'), index=True) -# how to reactivate the following two lines?! +# how to reactivate the following lines?! # station = relationship('StationmetaCore') # variable = relationship('Variable') +# da_vocabulary = relationship('DaVocabulary') +# at_vocabulary = relationship('AtVocabulary') +# mm_vocabulary = relationship('MmVocabulary') +# sf_vocabulary = relationship('SfVocabulary') +# ds_vocabulary = relationship('DsVocabulary') diff --git a/toardb/timeseries/models_role.py b/toardb/timeseries/models_role.py index eca3f3e376a0ba16b1f9cce159e1e1cece422daa..bf88739ed2a4e7a362fffa97e4de3e35fbad900f 100644 --- a/toardb/timeseries/models_role.py +++ b/toardb/timeseries/models_role.py @@ -51,13 +51,15 @@ class TimeseriesRole(Base): ) id = Column(Integer, TIMESERIES_ROLES_ID_SEQ, primary_key=True, server_default=TIMESERIES_ROLES_ID_SEQ.next_value()) - role = Column(Integer, nullable=False) - status = Column(Integer, nullable=False) + role = Column(ForeignKey('rc_vocabulary.enum_val'), nullable=False) + status = Column(ForeignKey('rs_vocabulary.enum_val'), nullable=False) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U person_id = Column(ForeignKey(Person.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True) - person = relationship(Person) +# person = relationship(Person) +# rc_vocabulary = relationship('RcVocabulary') +# rs_vocabulary = relationship('RsVocabulary') timeseries = relationship("Timeseries", secondary=timeseries_timeseries_roles_table, diff --git a/toardb/timeseries/schemas.py b/toardb/timeseries/schemas.py index 583be3a2ce0110433ae03979baa4fe15baa88a24..b89622b125a6e5487814c651a5f954e9e3769932 100644 --- a/toardb/timeseries/schemas.py +++ b/toardb/timeseries/schemas.py @@ -9,7 +9,7 @@ from typing import List from pydantic import BaseModel, Json, validator import datetime as dt from toardb.generic.models import RS_enum, RC_enum -from .models import DA_enum, SF_enum, AT_enum, DS_enum +from .models import DA_enum, SF_enum, AT_enum, DS_enum, MM_enum # ======== Timeseries ========= @@ -47,6 +47,10 @@ class TimeseriesCoreBase(BaseModel): def check_source(cls, v): return tuple(filter(lambda x: x.value == int(v), DS_enum))[0].string + @validator('measurement_method') + def check_measurement_method(cls, v): + return tuple(filter(lambda x: x.value == int(v), MM_enum))[0].string + class TimeseriesCoreCreate(TimeseriesCoreBase): pass @@ -79,6 +83,12 @@ class TimeseriesCoreCreate(TimeseriesCoreBase): else: raise ValueError(f"data source not known: {v}") + @validator('measurement_method') + def check_measurement_method(cls, v): + if tuple(filter(lambda x: x.string == v, MM_enum)): + return v + else: + raise ValueError(f"measurement method not known: {v}") class TimeseriesCore(TimeseriesCoreBase):