Skip to content

Commit

Permalink
chore: remove foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Jan 26, 2024
1 parent 338b184 commit 644f657
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dao/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (dao *dbPurchaseDao) Update(context context.Context, purchase *database.Pur

func (dao *dbPurchaseDao) Get(context context.Context, id int64) (database.Purchase, error) {
var purchase = database.Purchase{}
if err := dao.db.Preload("Item").Where("id = ?", id).Take(&purchase).Error; err != nil {
if err := dao.db.Preload("Item").Preload("Item.Stats").Where("id = ?", id).Take(&purchase).Error; err != nil {
return purchase, err
}
return purchase, nil
Expand Down
2 changes: 1 addition & 1 deletion database/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ type Category struct {
Id int64 `json:"id" gorm:"primaryKey;not null;autoIncrement:false"`
Name string `json:"name" gorm:"not null;size:32"`

Items []*Item `json:"items" gorm:"foreignKey:CategoryId"`
//Items []*Item `json:"items" gorm:"foreignKey:CategoryId"`
}
8 changes: 6 additions & 2 deletions database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ func Connect() (*gorm.DB, error) {

func ConnectDBWithConfig(config *util.DBConfig) (*gorm.DB, error) {
if config.DBDialect == "sqlite3" {
db, err := gorm.Open(sqlite.Open(config.DBPath), &gorm.Config{})
db, err := gorm.Open(sqlite.Open(config.DBPath), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
return db.Debug(), err
} else if config.DBDialect == "mysql" {
dbPath := fmt.Sprintf("%s:%s@%s", config.Username, config.Password, config.DBPath)
db, err := gorm.Open(mysql.Open(dbPath), &gorm.Config{})
db, err := gorm.Open(mysql.Open(dbPath), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
panic(err)
}
Expand Down
2 changes: 2 additions & 0 deletions database/data.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE SCHEMA IF NOT EXISTS `data-marketplace` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

insert into categories(id, name) values (100, 'Uncategorized');
insert into categories(id, name) values (1, 'AI Model');
insert into categories(id, name) values (2, 'Code Resource');
Expand Down
2 changes: 1 addition & 1 deletion database/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Item struct {
UpdatedGnfdHeight int64 `json:"updated_gnfd_height"`
UpdatedBscHeight int64 `json:"updated_bsc_height"`

Stats *ItemStats `json:"stats" gorm:"foreignKey:ItemId"`
Stats *ItemStats `json:"stats"`
}

type ItemStats struct {
Expand Down

0 comments on commit 644f657

Please sign in to comment.