Skip to content

Is there a way I can model relationships manually without code generation? #271

Answered by stephenafamo
theoribeiro asked this question in Q&A
Discussion options

You must be logged in to vote

The best way would be to comb through the generated code and write similar code yourself, although it may not be as easy to model relationships similar to Jet.

Here is what the final code would look like:

type User struct {
	ID    string `db:"id"`
	Name  string `db:"name"`
	Age   int    `db:"age"`
	Posts []Post `db:"-"`
}

type Post struct {
	ID        string    `db:"id"`
	UserID    string    `db:"user_id"` // needed to match the relationship
	Title     string    `db:"title"`
	CreatedAt time.Time `db:"created_at"`
}

func loadUserPosts(ctx context.Context, exec bob.Executor, retrieved any) error {
	users, ok := retrieved.([]User)
	if !ok {
		return fmt.Errorf("expected []*User, got %T", u…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by stephenafamo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants