Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define a nullable relation type with hasMany #101

Open
michaelboke opened this issue May 26, 2016 · 1 comment
Open

How to define a nullable relation type with hasMany #101

michaelboke opened this issue May 26, 2016 · 1 comment

Comments

@michaelboke
Copy link
Contributor

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.

@bketelsen
Copy link
Member

My suggestion: try again after #118 lands and see if that fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants