first version of db model

This commit is contained in:
2025-04-14 17:07:12 +02:00
parent 0564489b95
commit 2b1e2f76c6
4 changed files with 137 additions and 75 deletions

View File

@@ -39,6 +39,8 @@ func dbMigration() {
db.Create(&models.AssetType{Name: "field"}) db.Create(&models.AssetType{Name: "field"})
db.Create(&models.AssetType{Name: "animal"}) db.Create(&models.AssetType{Name: "animal"})
db.Create(&models.AssetType{Name: "substance"}) db.Create(&models.AssetType{Name: "substance"})
err := db.SetupJoinTable(&models.Fertilizer{}, "Ingredients", &models.FertilizerIngredient{})
} }
func main() { func main() {

View File

@@ -11,7 +11,6 @@ type Land struct {
FID string `gorm:"not null"` FID string `gorm:"not null"`
IsWaterProtectionArea bool `gorm:"not null"` IsWaterProtectionArea bool `gorm:"not null"`
IsRedArea bool `gorm:"not null"` IsRedArea bool `gorm:"not null"`
Betriebsnummer?
Area float64 `gorm:"not null"` Area float64 `gorm:"not null"`
} }
@@ -20,47 +19,69 @@ type Plant struct {
LandId uint `gorm:"not null"` LandId uint `gorm:"not null"`
Land Land `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Land Land `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CropID uint `gorm:"not null"` CropID uint `gorm:"not null"`
Crop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Crop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CoverCropID uint `gorm:"not null"` CoverCropID uint `gorm:"not null"`
CoverCrop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` CoverCrop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Year uint `gorm:"not null"` Year uint `gorm:"not null"`
AreaPercentage float64 `gorm:"not null"` AreaPercentage float64 `gorm:"not null"`
} }
type _Resource struct {
type Machine struct {
_Asset _Asset
UnitID uint `gorm:"not null"` UnitID uint `gorm:"not null"`
Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Unit Unit `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro Cost decimal.Decimal `gorm:"not null; type:decimal(10,2);"` // in euro/unit
} }
type SeedType struct { type Machine struct {
ID uint `gorm:"primaryKey` _Resource
Name string `gorm:"not null"` }
CropID uint `gorm:"not null"`
Crop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` type _BoughtResource struct {
_Resource
BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"`
BoughtFromID uint `gorm:"not null"`
BoughtFrom Seller `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
type _SoldResource struct {
_Resource
SaleDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"`
SoldToID uint `gorm:"not null"`
SoldTo Customer `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
} }
type Seed struct { type Seed struct {
_Asset _BoughtResource
BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"`
UnitID uint `gorm:"not null"`
Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro/unit
SourcedFromID uint `gorm:"not null"`
SourcedFrom Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
SeedTypeID uint `gorm:"not null"` SeedTypeID uint `gorm:"not null"`
SeedType SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` SeedType SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
} }
type Fertilizer struct { type Fertilizer struct {
_Asset _Asset
BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"` _BoughtResource
UnitID uint `gorm:"not null"`
Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro/unit
SourcedFromID uint `gorm:"not null"`
SourcedFrom Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
FertilizerTypeID uint `gorm:"not null"` FertilizerTypeID uint `gorm:"not null"`
FertilizerType Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` FertilizerType FertilizerType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
// Association to the ingredients. This represents the composition.
Ingredients []FertilizerIngredient `gorm:"not null; many2many:fertilizer_ingredient"`
}
type FertilizerIngredient struct {
FertilizerAssetID uint `gorm:"primaryKey"` // foreign key to the fertilizer asset
IngredientID uint `gorm:"primaryKey"` // foreign key to the ingredient
Amount float64 `gorm:"not null"` // amount of the ingredient
// Preload the ingredient details if needed.
Ingredient Ingredient `gorm:"not null"`
}
type Pesticide struct {
_BoughtResource
PesticideTypeID uint `gorm:"not null"`
PesticideType PesticideType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
type Product struct {
_SoldResource
ProductTypeID uint `gorm:"not null"`
ProductType ProductType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
} }

View File

@@ -1,12 +1,14 @@
package models
type Log struct { type Log struct {
ID uint `gorm:"primaryKey"` ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Notes string Notes string `gorm:"not null"`
LogType string `gorm:"not null"` LogType string `gorm:"not null"`
Date Datetime.Time `gorm:"type:date;default:CURRENT_DATE"not null` Date Datetime.Time `gorm:"type:date;default:CURRENT_DATE"not null`
Duration float64 `gorm:"not null"` // in hours Duration float64 `gorm:"not null"` // in hours
VisibiltyID uint `gorm:"not null"` VisibiltyID uint `gorm:"not null"`
Visibility Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Visibility Visibility `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
} }
type _LogMetadata struct { type _LogMetadata struct {
@@ -27,15 +29,31 @@ type SeedingLog struct {
SeedID uint `gorm:"not null"` SeedID uint `gorm:"not null"`
Seed Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Seed Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
TechniqueID uint `gorm:"not null"` TechniqueID uint `gorm:"not null"`
Technique Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Technique SeedingTechnique `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
Amount float64 `gorm:"not null"` // in units/ha Amount float64 `gorm:"not null"` // in units/ha
ConditionsID uint `gorm:"not null"` ConditionsID uint `gorm:"not null"`
Conditions Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Conditions SeedingCondition `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
} }
type FertilizingLog struct { type FertilizingLog struct {
_FieldWorkLog _FieldWorkLog
FertilizerID uint `gorm:"not null"` FertilizerID uint `gorm:"not null"`
Fertilizer Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Fertilizer Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
Amount float64 `gorm:"not null"` // in units/ha Amount float64 `gorm:"not null"` // in units of the fertilizer
}
type CropProtectionLog struct {
_FieldWorkLog
PesticideID uint `gorm:"not null"`
Pesticide Pesticide `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
Amount float64 `gorm:"not null"` // in units of the pesticide
PerformedByID uint `gorm:"not null"`
PerformedBy Worker `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
}
type HarvestLog struct {
_FieldWorkLog
ProductID uint `gorm:"not null"`
Product Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
Amount float64 `gorm:"not null"` // in units of the product
} }

View File

@@ -1,7 +1,28 @@
package models package models
type Taxonomy struct { type _Taxonomy struct {
ID uint `gorm:"primaryKey" json:"id"` ID uint `gorm:"primaryKey" json:"id"`
Type string `gorm:"not null;index" json:"type"` // e.g., "unit", "color", etc.
Name string `gorm:"not null;index" json:"name"` // e.g., "kg", "lb" Name string `gorm:"not null;index" json:"name"` // e.g., "kg", "lb"
} }
type Unit struct{ _Taxonomy }
type Visibility struct{ _Taxonomy }
type SeedingTechnique struct{ _Taxonomy }
type SeedingCondition struct{ _Taxonomy }
type Worker struct{ _Taxonomy }
type Crop struct{ _Taxonomy }
type Seller struct{ _Taxonomy }
type Customer struct{ _Taxonomy }
type SeedType struct {
_Taxonomy
CropID uint `gorm:"not null"`
Crop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
type Ingredient struct {
_Taxonomy
UnitID uint `gorm:"not null"`
Unit Unit `gorm:"not null"`
}
type FertilizerType struct{ _Taxonomy }
type PesticideType struct{ _Taxonomy }
type ProductType struct{ _Taxonomy }