goadesign / goadesign/gorma

How to define a nullable relation type with hasMany

Open
#101 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
141
Forks
35
PR merge metrics
No merged PRs in 30d

Description

I got the following usecase:
I got books and Shelfs, Books can be assigned to Shelfs.
There is the case a book isnt assigned to a shelf and there for the shelf_id should be nil

I tried the Nullable command on a Field, but does not work.

```
Model("Shelf", func() {

Field("id", gorma.Integer, func() {
PrimaryKey()
})

HasMany("Books", "Book")
)}

Model("Book", func() {

Field("id", gorma.Integer, func() {
PrimaryKey()
})

//Hack not working
Field("shelf_id", gorma.Integer, func() {
Nullable()
})
})
```

This will generate a model like:

```
type Book struct {
ID int `gorm:"primary_key"` // primary key
ShelfID int // has many Book
}

type Shelf struct {
ID int `gorm:"primary_key"` // primary key
Books []Book // has many Books
}
```

But what i actualy want is (note the pointer to *int)

```
type Book struct {
ID int `gorm:"primary_key"` // primary key
ShelfID *int // has many Book
}
```

How can i achieve this? or is this even possible.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.