We currently have the following schema;
type Reactions []*Reaction
type Post struct {
ID string `pg:",pk" json:",omitempty"`
CreatorID string `pg:",notnull"`
Creator *User `pg:",rel:has-one,fk:creator_id"`
Groups Groups
Reactions Reactions `pg:",rel:has-many" json:",omitempty"`
}
type Reaction struct {
ID string `pg:",pk" json:",omitempty"`
Type ReactionType `pg:",notnull"`
CreatorID string `pg:",notnull"`
Creator *User `pg:",rel:has-one,fk:creator_id"`
PostID string `pg:",notnull"`
Post *Post `pg:",rel:has-one,fk:post_id"`
}
When trying to query all Posts including their Reaction Relations using the following query, we recieve the following error message; pg: relation=\"Reactions\" does not have base model=Post with id=\"\" (check join conditions)
func (pm PGPostRepo) selectQuery(model ...interface{}) *orm.Query {
return pm.db.Model(model...).
Relation("Creator.id").
Relation("Creator.given_name").
Relation("Creator.family_name").
Relation("Reactions.type").
Column("post.*")
Where("post.id = ?", postID).
Select()
}
I'm actually quite lost on this one, as if we replace
Relation("Reaction.type") with Relation("Reaction.*") we do not get the error(Though both Creator & Post are null), but then we're retrieving more columns than we'd like.
I am not a pro but isn't it the Reactions in Post model. It should be Reactions Reaction ... instead of Reactions Reactions .... Because the model is Reaction, not Reactions. I hope it solves the problem and i am not a stupid.
Related
I have the following model:
type Drink struct {
gorm.Model // Adds some metadata fields to the table
ID uuid.UUID `gorm:"type:uuid;primary key"`
Name string `gorm:"index;not null;"`
Volume float64 `gorm:"not null;type:decimal(10,2)"`
ABV float64 `gorm:"not null;type:decimal(10,2);"`
Price float64 `gorm:"not null;type:decimal(10,2);"`
Location Location `gorm:"ForeignKey:DrinkID;"`
}
type Location struct {
gorm.Model // Adds some metadata fields to the table
ID uuid.UUID `gorm:"primary key;type:uuid"`
DrinkID uuid.UUID
Name string `gorm:"not null;"`
Address string `gorm:"not null;type:decimal(10,2)"`
Phone int `gorm:"not null;type:decimal(10,0);"`
}
however, when I run the program, it adds both tables, however there is no location field in the Drink table.
My database looks like this after the migrations, regardless of whether I drop the tables previously:
I have a sneaking feeling it might be because I am not using the gorm default ID, but if that's the case can anyone point me to how to override the default ID with a UUID instead of a uint the proper way? or if that's not even the issue, please, I've been working on this for a few days now and I really don't want to take the "easy" road of just using the defaults gorm provides, I actually want to understand what is going on here and how to properly do what I am trying to do. I am getting no errors when running the API, and the migration appears to run as well, it's just the fields I have defined are not actually showing up in the database, which means that the frontend won't be able to add data properly.
What I WANT to happen here is that a list of stores will be available in the front-end, and when a user adds a drink, they will have to select from that list of stores. Each drink added should only have 1 store, as the drinks prices at different stores would be different. So technically there would be many "repeated" drinks in the drink table, but connected to different Locations.
First point is as you are using custom primary key, you should not use gorm.Model as it contains ID field in it. Reference
Second point is according to your description, store (location) has one to
many relationship with drink. That means a store can have multiple
drinks but a drink should belong to only one store. In one-to-many
relationship there should be a reference or relation id in the many
side. That means in your case in drink table. Then your struct
should look like this:
MyModel Struct
type MyModel struct {
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
Location Struct (Store)
type Location struct {
MyModel
ID uuid.UUID `gorm:"primary key;type:uuid"`
// other columns...
Drinks []Drink
}
Drink Struct
type Drink struct {
MyModel
ID uuid.UUID `gorm:"type:uuid;primary key"`
//other columns...
LocationID uuid.UUID// This is important
}
Then gorm will automatically consider LocationID in drink table will be referring the ID field of Location Table. You can also explicitly instruct this to gorm using gorm:"foreignKey:LocationID;references:ID" in Location struct's Drinks array field.
Reference
I'm using GORM for MongoDB in my Grails 3 web-app to manage read/writes from DB.
I have the following 2 domain classes:
class Company {
String id
}
class Team {
String id
Company company
}
For teams, their company is saved on DB as String, and with GORM I can simply use team.company to get an instance of Company domain class.
However, I need to override the getter for company, and I need the raw value for company id (as stored on DB), without GORM getting in the way and performing its magic.
Is there a way to get the raw String value?
Any help is welcome! Thanks in advance
Update (May 27)
Investigating #TaiwaneseDavidCheng suggestion, I updated my code to
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
company attr: "company" // optional
companyId attr: "company", insertable: false, updateable: false
}
}
Please note that I'm using GORM for MongoDB, which (citing the manual) tries to be as compatible as possible with GORM for Hibernate, but requires a slightly different implementation.
However I found out (by trial&error) that GORM for MongoDB doesn't support a similar solution, as it seems only one property at a time can be mapped to a MongoDB document property.
In particular the last property in alphabetical order wins, e.g. companyId in my example.
I figured out a way to make the whole thing work, I'm posting my own answer below.
given a non-insertable non-updateable column "companyId" in domain class
class Company {
String id
}
class Team {
String id
Company company
Long companyId
static mapping = {
company column:"companyId"
companyId column:"companyId",insertable: false,updateable: false
}
}
(Follows the edit to my question above)
I defined a custom mapping, and made use of Grails transients by also defining custom getter and setter for team's company.
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
companyId attr: "company" // match against MongoDB property
}
static transients = [ 'company' ] // non-persistent property
Company getCompany() {
return Company.get(companyId)
}
void setCompany(Company company) {
companyId = company.id
}
}
I have a Task type that has a list of Runner type objects in it. I am trying to map it to database using golang gorm but it doesn't have foreign key and i am getting invalid association during migration
My Task struct:
type Task struct {
gorm.Model
Name string `gorm:"not null;unique_index"`
Description string
Runners []Runner
}
My Runner struct:
type Runner struct {
gorm.Model
Name string `gorm:"not null;unique"`
Description string
}
My migration code:
func migrateSchema () (err error) {
db, err := context.DBProvider()
if err != nil {
return
}
db.Model(&Task{}).Related(&Runner{})
db.AutoMigrate(&Task{})
db.AutoMigrate(&Runner{})
return
}
On db.AutoMigrate(&Task{}) I get invalid association message in console and when I check the database there is no foreign key created or no reference field created on runners table
What am I doing wrong?
I had a similar issue, and it took me forever to figure it out. I believe the GORM documentation could definitely be better. Here's the relevant code snippet from the GORM site:
//User has many emails, UserID is the foreign key
type User struct {
gorm.Model
Emails []Email
}
type Email struct {
gorm.Model
Email string
UserID uint
}
db.Model(&user).Related(&emails)
//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key
Why your code isn't working:
First you need to add a TaskID field to your Runner struct.
db.Model(&Task{}).Related(&Runner{}) doesn't do what you think it does. If you look at the code snippet from GORM, the SELECT comment kind of explains it (not very well though). The example is assuming that the &user is already populated and has an ID of 111, then it fetches the emails storing them in &emails that match the UserID of &user.
I've got 2 structs to represent a ManyToMany relationship. User and Note
type User struct {
ID int
Name string
Notes []*Note
}
type Note struct {
TableName struct{} `sql:"user_notes"`
ID int
Text string
}
Now let's say I want to insert a new user and also at the same time add a few notes.
I would expect this to insert a user and its note(s):
note := Note{
Text: "alohaa dude",
}
user := User{
Name: "peter",
Notes: []Note{no},
}
s.DB.Insert(&user)
However this only saves the user and not the user and the note. In go-pg do I have to do this manually or is there an automated way through the ORM?
Rodrigo, same problem statement is being discussed here: https://github.com/go-pg/pg/issues/478
This functionality is not supported in go-pg at this time and you might want to try a db prepare approach to insert with relationships.
I'm currently working on a small application that allows users to rate certain objects, my database (MongoDB) structure looks like this
Movie {
Id int
Name string
}
Actor {
Id int
Name string
Age int
}
Movie_Actors {
Movie Movie
Actor Actor
}
User {
Id int
Username string
Password string
}
Rating {
Id int
User User
Actor Actor
Rating int
}
My problem begins when I want to select all Actors in a Movie where there are less than 5 Ratings
// db *mgo.Database
c := db.C('ratings')
err := c.Find(...)
For this problem, I would denormalize your schema a little to make querying in MongoDB more natural. Looking only at the types you need for this query, you might use the following:
type Movie struct {
Id bson.ObjectId `_id,omitempty`
Name string
Actors []bson.ObjectId
}
type Actor struct {
Id bson.ObjectId `_id,omitempty`
Name string
Age int
Ratings []Rating
NRatings int
}
Here the Movie directly embeds the Actor IDs, and the Actor keeps track of the number of ratings in a separate field from the ratings themselves. If you only wanted to run this single type of query, you could fully denormalize the data model by embedding the Movies directly into the Actors who played in them. This would simplify the query even more, but would also create a lot of duplication of information and prevent you from dealing with Movies as separate entities.
See the documentation on schema design for a discussion on these tradeoffs.
In this design, you can find the actors in the movie Fantastic Mr. Fox with fewer than 5 ratings using the following:
// Find the actors in the movie
var m Movie
db.C("movies").Find(bson.M{"name": "Fantastic Mr. Fox"}).One(&m)
// Filter the actors to find those with less than 5 ratings.
var results []Actor
db.C("actors").Find(bson.M{"_id": bson.M{"$in": m.Actors}, "nratings": bson.M{"$lt": 5}}).All(&results)
fmt.Println(results)