From e000a518e3d0634f555872d95bca341b1f015c08 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 28 Apr 2019 16:50:39 +0200 Subject: [PATCH] Breaks down the associations of dummyDB to groups: The three different groups are : - HasMany and BelongsTo - HasMany - BelongsTo For the first category only the BelongsTo associations are created and the HasMany are going to automatically retrieved since we have the same foreign key as the BelongsTo --- common/database.go | 63 ++++++++++++++++++++++++------------------- common/datastructs.go | 6 ++--- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/common/database.go b/common/database.go index 6e805b3..1bbb85a 100644 --- a/common/database.go +++ b/common/database.go @@ -130,37 +130,46 @@ func DummyPopulateDB(test_db *gorm.DB) { // For `belongs to` use the model with id=1 // For `has many` use the models with id=1 and id=2 - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) - - checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) - checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_A).Error) - checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_B).Error) - checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_A).Error) - checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_B).Error) - - checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error) - checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error) - checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_A).Error) - checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_B).Error) - - checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_A).Error) - checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_B).Error) - checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_A).Error) - checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_B).Error) - checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_A).Error) - checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_B).Error) - + // Project HM Visualization, Visualization BT Project checkErr(test_db.Model(&vis_A).Association("Project").Append(&proj_A).Error) - checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&vis_B).Association("Project").Append(&proj_A).Error) + + // User HM Project, Project BT User + checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&proj_B).Association("User").Append(&usr_A).Error) + + // Simulation HM Project, Project BT Simulation + checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error) + checkErr(test_db.Model(&proj_B).Association("Simulation").Append(&simn_A).Error) + + // User HM File, File BT User + checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&file_B).Association("User").Append(&usr_A).Error) + + // Simulation HM SimModel, SimModel BT Simulation + checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) + checkErr(test_db.Model(&smo_B).Association("BelongsToSimulation").Append(&simn_A).Error) + + // User HM Simulation, Simulation BT User + checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&simn_B).Association("User").Append(&usr_A).Error) + + // Visualization HM Widget checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_A).Error) checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_B).Error) - checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error) + // SimModel HM Signal + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) + + // Visualization BT User + checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) + + // Simulator BT SimModel + checkErr(test_db.Debug().Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) + } // Erase tables and glose the testdb diff --git a/common/datastructs.go b/common/datastructs.go index b68dc8a..6d4dd9c 100644 --- a/common/datastructs.go +++ b/common/datastructs.go @@ -77,8 +77,8 @@ type SimulationModel struct { BelongsToSimulatorID uint `gorm:"not null"` // NOTE: order of signals is important - OutputMapping []Signal `gorm:"association_autoupdate:false"` - InputMapping []Signal `gorm:"association_autoupdate:false"` + OutputMapping []Signal `gorm:""` + InputMapping []Signal `gorm:""` } type User struct { @@ -106,7 +106,7 @@ type Visualization struct { User User `gorm:"not null;association_autoupdate:false"` UserID uint `gorm:"not null"` - Widgets []Widget `gorm:"association_autoupdate:false"` + Widgets []Widget `gorm:""` } type Signal struct {