Just started learning gorm a few days ago, I have figured out 90% of my sql schema, just can't quite add answer_id foreign key in the posts table. Does anyone know what I'm doing wrong?
This is more or less the sql schema
create table tags (
id serial primary key,
tag varchar(35)
);
create table posts (
id int primary key,
answer_id int,
title varchar(150),
file_offset int
--foreign key (answer_id)
--references answers (id)
);
create table post_tags (
post_id int not null references posts(id),
tag_id int not null references tags(id),
primary key (post_id, tag_id)
);
create table answers (
id int primary key,
parent_id int not null references posts(id),
file_offset int
);
ALTER table posts
ADD CONSTRAINT "primary_fkey"
FOREIGN KEY ("answer_id")
REFERENCES answers("id")
ON UPDATE CASCADE;
What I currently have for gorm, all fk are good except I can't figure out answer_id. I've tried a few things but can't seem to get it when I check the db, the answer_id column is just an int without a fk.
type BaseRow struct {
ID uint `xml:"Id,attr"`
Offset int
Length int
}
type Post struct {
BaseRow
AnswerID uint `gorm:"default:null"`
Answers []Answer
Title string `gorm:"type:varchar(150)"`
Tags []Tag `gorm:"many2many:post_tags"`
//Answer Answer `gorm:"foreignKey:ID;references:answer_id"`
}
type Answer struct {
BaseRow
PostID uint
}
type Tag struct {
ID uint
Name string `gorm:"type:varchar(35);unique"`
}