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

@@ -28,17 +28,19 @@ func initLogging() {
} }
func dbMigration() { func dbMigration() {
database.DB.AutoMigrate( database.DB.AutoMigrate(
&models.AssetType{}, &models.AssetType{},
&models.Asset{}, &models.Asset{},
&models.LogType{}, &models.LogType{},
&models.Log{}, &models.Log{},
) )
db.Create(&models.AssetType{Name: "machine"}) db.Create(&models.AssetType{Name: "machine"})
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

@@ -1,66 +1,87 @@
package models package models
type _Asset struct { type _Asset struct {
ID uint `gorm:"primaryKey` ID uint `gorm:"primaryKey`
Name string `gorm:"not null"` Name string `gorm:"not null"`
Notes string Notes string
} }
type Land struct { type Land struct {
_Asset _Asset
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"`
} }
type Plant struct { type Plant struct {
_Asset _Asset
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 {
_Asset
UnitID uint `gorm:"not null"`
Unit Unit `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost decimal.Decimal `gorm:"not null; type:decimal(10,2);"` // in euro/unit
} }
type Machine struct { type Machine struct {
_Asset _Resource
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
} }
type SeedType struct { type _BoughtResource struct {
ID uint `gorm:"primaryKey` _Resource
Name string `gorm:"not null"` BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"`
CropID uint `gorm:"not null"` BoughtFromID uint `gorm:"not null"`
Crop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` 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"` SeedTypeID uint `gorm:"not null"`
UnitID uint `gorm:"not null"` SeedType SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
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"`
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"` FertilizerTypeID uint `gorm:"not null"`
Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` FertilizerType FertilizerType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro/unit // Association to the ingredients. This represents the composition.
SourcedFromID uint `gorm:"not null"` Ingredients []FertilizerIngredient `gorm:"not null; many2many:fertilizer_ingredient"`
SourcedFrom Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` }
FertilizerTypeID uint `gorm:"not null"`
FertilizerType Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` 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,41 +1,59 @@
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 {
ID uint `gorm:"primaryKey"` ID uint `gorm:"primaryKey"`
LogID uint `gorm:"not null"` LogID uint `gorm:"not null"`
Log Log `gorm:"foreignKey:LogID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Log Log `gorm:"foreignKey:LogID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
} }
type _FieldWorkLog struct { type _FieldWorkLog struct {
_LogMetadata _LogMetadata
WorkedAreaPercentage float64 `gorm:"not null"` WorkedAreaPercentage float64 `gorm:"not null"`
MachineID uint `gorm:"not null"` MachineID uint `gorm:"not null"`
Machine Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Machine Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
} }
type SeedingLog struct { type SeedingLog struct {
_FieldWorkLog _FieldWorkLog
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 }