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

Add ability to define foreign keys in models #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add ability to define foreign keys in models #67

wants to merge 3 commits into from

Conversation

adam-frisby
Copy link

Adds a ForeignKeyed interface similar to the Index interface for defining foreign keys in models. (It only supports single column foreign keys)


Example Schemas

type User struct {
    Id   hood.Id
    Name string
}

type Post struct {
    Id      hood.Id
    Content string
    UserId  int64
}

func (table *Post) ForeignKeys(foreignKeys *hood.ForeignKeys) {
    foreignKeys.Add("test_fk", "user_id", "user", "id", hood.Cascade, hood.Cascade)
}

I added a ReferentialAction type. Cascase, Restrict, NoAction, and SetNull are constants defined. It follows the same pattern that Join does.

The function signature for foreignKeys.Add is

Add(name string, column string, referenceTable string, referenceColumn string, onUpdate ReferentialAction, onDelete ReferentialAction)

name: Name of foreign key constraint
column: The column in the table/model to create the foreign key on
referenceTable: The name of the table with the column the foreign key will reference
referenceColumn: The column in the referenceTable the foreign key will reference

Two functions were added to dialect.go: ForeignKey and ReferentialAction.

Foreign keys are created when the table is created.

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

Successfully merging this pull request may close these issues.

1 participant