Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 17:53
    wregis assigned #4463
  • 17:53
    wregis labeled #4463
  • 17:53
    wregis opened #4463
  • 15:51
    Refuel-ZL edited #4374
  • 15:29
    github-actions[bot] closed #4459
  • 13:19

    jinzhu on v1.21.11

    (compare)

  • 02:21
    github-actions[bot] labeled #4274
  • 02:21
    github-actions[bot] commented #4274
  • Jun 14 21:19
    s-takehana opened #435
  • Jun 14 17:09
    therealpaulgg commented #3702
  • Jun 14 14:33
    daheige synchronize #4460
  • Jun 14 14:26
    daheige synchronize #4460
  • Jun 14 14:21
    daheige synchronize #4460
  • Jun 14 14:13
    daheige synchronize #4460
  • Jun 14 11:33
    github-actions[bot] labeled #4462
  • Jun 14 11:33
    github-actions[bot] commented #4462
  • Jun 14 11:26
    github-actions[bot] labeled #4462
  • Jun 14 11:26
    pankajsoni19 assigned #4462
  • Jun 14 11:26
    pankajsoni19 opened #4462
  • Jun 14 09:27
    jinzhu synchronize #428
Hadrien Dussuel-Jarry
@dbkblk_gitlab
Hello. I have a warning in my logs that I cannot get ride off about the connection. I could not find any information in the docs or on google:
I connect a mariadb windows instance with this exact structure: "user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True"
It works, but the log output "WARN: You specified both instance name and port in the connection string, port will be used and instance name will be ignored".
I don't understand what is wrong by doing that? Could you help me please? :)
Hadrien Dussuel-Jarry
@dbkblk_gitlab
My bad, I think it comes from another module. Sorry about that.
Hadrien Dussuel-Jarry
@dbkblk_gitlab
(It was from sqlx, sorry)
Hadrien Dussuel-Jarry
@dbkblk_gitlab
For future reference, it is not about sqlx neither but about a mssql server (because I also connect to one). To connect, either specify a port or an instance (/instance) which seems obvious now when I read the warning. I just didn't know #feelStupid
Fl0w
@fl0wx
hey everyone. short question. is it possible / how is it possible to set a role before doing migraitons with postgresql backend?
The Promised Prince <Deba>
@light-bringer

hey, everyone

how do I ensure in gorm that the foriegn key exists in the referenced table for a one to one relationship.

Chirag Shah
@ichiragshah

Can someone explain - To ORM or not to ORM in GoLang?

Here's what I have read -
Consider the acronym ORM: Object-Relational Mapping. It presupposes an object-oriented language, which Go emphatically is not. There are packages available for Go that attempt to do some of the same things that a true ORM does for an object-oriented language, but they're all both inefficient and incomplete. They all rely on reflection (notoriously slow), and I have yet to come across one that really maps relationships between tables into relationships between Go types.

So is it advisable to use GORM?

L1ghtman2k
@L1ghtman2k

Has anyone been working on gorm + protobuf?
My main dilemma is whether I should separate model structs from protobuf structs.

Say I have API that consumes a protobuf message, in order to store it in the database.
Would you have a separate struct for your storage layer, aka you convert protobuf struct, to a native storage struct using custom methods, or would you instead reuse protobuf struct directly, and instead use some project like https://github.com/favadi/protoc-go-inject-tag to generate gorm structs on protobuf fields?

熊大山
@xiongdashan
hey evveryone, how to modify a column type in v2
bignew.wang
@bignew-wang
hi
Pacharapol Withayasakpunt
@patarapolw
GormDataType required to make custom map[string]interface{} work, but undocumented. So, I had to look at https://github.com/go-gorm/datatypes to make JSON for SQLite work...
Important line,
go
// GormDataType gorm common data type
func (JSONObject) GormDataType() string {
    return "jsonObject"
}
Pacharapol Withayasakpunt
@patarapolw
Another problem. This doesn't work tx.Unscoped().Delete(&db.Card{}), but this does tx.Unscoped().Where("TRUE").Delete(&db.Card{})
How do I purge ALL soft deletes?
Blue King
@bluekinginc:matrix.org
[m]

I need to use Gorm/postgres persisting date array. It just doesn't work for me for some reason. There was no error but no content was saved (empty when I dump the table).

My definition was like:
type History struct {
Dates []time.Time gorm:"type:date[]" json:"dates"
}

Is there a working example somewhere that I can take a look?

Thanks!

Blue King
@bluekinginc:matrix.org
[m]

:point_up: Edit: I need to use Gorm/postgres to persist date array. It just doesn't work for me for some reason. There was no error but no content was saved (empty when I dump the table).

My definition was like:
type History struct {
Dates []time.Time gorm:"type:date[]" json:"dates"
}

Is there a working example somewhere that I can take a look?

Thanks!

Blue King
@bluekinginc:matrix.org
[m]

:point_up: Edit: I need to use Gorm/postgres to persist date array. It just doesn't work for me for some reason. There was no error but no content was saved (empty when I dump the table).

My definition was like:
type History struct {
Dates []time.Time gorm:"type:date[]" json:"dates"
}

Is there a working example somewhere that I can take a look?

Thanks!

:point_up: Edit: I need to use Gorm/postgres to persist date array. It just doesn't work for me for some reason. There was no error but no content was saved (empty when I dump the table).

My definition was like:
type History struct {
Dates []time.Time gorm:"type:date[]" json:"dates"
}

Is there a working example somewhere that I can take a look?

Thanks!

Update: I finally got around and did the following, and it appears to be working. I convert the StringArray into []time.Time.

Dates pq.StringArray gorm:"column:dates_val;type:date[]" json:"dates"

:point_up: Edit: I need to use Gorm/postgres to persist date array. It just doesn't work for me for some reason. There was no error but no content was saved (empty when I dump the table).

My definition was like:
type History struct {
Dates []time.Time gorm:"type:date[]" json:"dates"
}

Is there a working example somewhere that I can take a look?

Thanks!

Update: I finally got around and did the following, and it appears to be working. I convert the StringArray into []time.Time.

Dates pq.StringArray gorm:"column:dates;type:date[]" json:"dates"

vuhoanglam
@vuhoanglam
image.png
@jinzhu due to this, we cannot alter the count result using hook/plugin right?
i'm writing a cache plugin, everything work well except the count() finisher
wbbinStudy
@wbbinStudy
@jinzhu Why field of CategoryID is Null,Why is not 0?
image.png
sneakycrow
@sneakycrow:matrix.org
[m]
Hey guys, hopefully this is an easy question: I have two resources: A project and a user. A project has many users. I need to get a list of projects containing a specific user. Is there a way to do that with association mode? I only see a way to return the user, not to return the project containing the user.
jonah
@jonah:matrix.psytux.com
[m]
You need to get the user PK first and then you a Where() function and only query to projects containing the users pk
sneakycrow
@sneakycrow:matrix.org
[m]
Should something like this work then? database.Model(&Project{}).Where("id = ?", user.ID).Association("Users").Find(&projects)
(I'm not at my computer for a few days or I'd try lol)
Manish Kumar
@manish.kumar5_gitlab
Hey guys,
I'm trying to create tables under different schema in MSSql
Anyone have idea how to do it?
Tobias
@teletobbie

Hello,

I am implementing some delete functionality using GORM. My idea is to cascade delete where when the parent row is deleted, the childern are deleted aswell. I have tried the delete with select. But it only deletes the parent record.

func DeleteProject(db *gorm.DB, id uint) (Project, error) {
    var project Project
    if err := db.Select(clause.Associations).Delete(&project, id).Error; err != nil {
        return Project{}, err
    }
    return project, nil
}

I have the following model:

type Project struct {
    ID           uint           `gorm:"primary_key" json:"id"`
    CreatedAt    time.Time      `json:"created_at"`
    UpdatedAt    time.Time      `json:"updated_at"`
    DeletedAt    gorm.DeletedAt `gorm:"index" json:"deleted_at"`
    TicketNumber string         `gorm:"size:6;unique;not null" json:"ticketnumber"`
    Name         string         `json:"name"`
    MailingList  string         `json:"mailinglist"`
    Environment  string         `gorm:"not null" json:"environment"`
    UserID       uint           `gorm:"index" json:"user_id"`
    Diagrams     []Diagram      `json:"diagrams"`
}

type Diagram struct {
    ID        uint           `gorm:"primary_key" json:"id"`
    CreatedAt time.Time      `json:"created_at"`
    UpdatedAt time.Time      `json:"updated_at"`
    DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
    Data      string         `json:"data"`
    ProjectID uint           `gorm:"index" json:"project_id"`
    Instances []Instance     `json:"instances"`
}

type Instance struct {
    ID        uint           `gorm:"primary_key" json:"id"`
    CreatedAt time.Time      `json:"created_at"`
    UpdatedAt time.Time      `json:"updated_at"`
    DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
    Key       uint           `json:"key"`
    DiagramID uint           `gorm:"index" json:"diagram_id"`
}

Where a project can have multiple diagrams and a diagram can have multiple instances. But when I am running the DeleteProject function it is only deleting the Project and not the associated diagrams and instances.

How can I delete a project and it's associated diagrams and instances in this case?

Thank you!
Lucas Pinto
@luckspt

Hi! I'm having a bit of trouble selecting multiple rows.
I have defined User as

type User struct {
    ID        string `gorm:"primaryKey"`
    Name      string
    IsAdmin   bool `gorm:"default:false"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt gorm.DeletedAt
}

and am trying to get all users with

var users []models.User
result := db.GetDB().Find(&users)

but keep getting runtime error: invalid memory address or nil pointer dereferenceon the last line

jonah
@jonah:matrix.psytux.com
[m]
Try something like db.Model(&models.User{}).Find(&users)
Lucas Pinto
@luckspt
Thanks @jonah:matrix.psytux.com but I forgot to update my message. I have fixed it - db.GetDB() was nil/undefined (I'm just starting with go)
ConaGo
@ConaGo

Hi there! I need some help with a simple query.

These are my data models(simplified)

type Conference struct {
    gorm.Model                        `json:"-"`
    Name      string             
    Tags      []Tag                 `gorm:"many2many:conference_tags;"`

}

type Tag struct {
    gorm.Model                        `json:"-"`
    TagName       string
    Conferences []Conference        `gorm:"many2many:conference_tags;"`
}

My Goal is to query so that:
given a list of tags
a list of conferences that are connected to the given tags is returned

My attempts were to preload the tags into the conference table
starting with this query i was not able to access the tagnames and filter by those

db.Preload("Tags").Find(&conferences)
db.Preload("Tags").Where("tag_name IN ?", []string{"javascript", "css", "devops"}).Order("start_date ASC").Find(&conferences) //fails

second query outupts cannot find table named tag_name
(this example is just to illustrate my problem.. Ive tried multiple things and failed)

second attempt was to start with the tag table

db.Where("tag_name IN ?", []string{"javascript", "css", "devops"}).Preload("Conferences").Group("tag_name").Find(&tags)

While this neatly groups conferences to tags it is also a one-to-many association and not how i need my data

my desired result looks like this

Result=[
    {
        Name:CSS Day,
        Tags:
        [
            {
                TagName:css
            },
            {
                TagName:html
            }
        ]
    },
    {
        Name:Devops Day,
        Tags: [
            {
                TagName:devops
            }
        ]
    }
]
.....

Any help is greatly appreciated :)

Sivakumar
@kumarsiva07
DB.model is not resetting the table name after find. I tried to find from table1 and want update table2 using same db object. But update query formed with table1 instead of table2
doppl-neal
@doppl-neal
Can you post some code?
Lucas Pinto
@luckspt
[postgres] Hi! I'm having an issue with a query. My query is the following: res := db.GetDB().Model(&db.User{}).First(&user, "id = ?", userID) and then resolves to SELECT * FROM "users" WHERE id = 'tHi9KeXYpMhqyKSUUpmkBpK0Z9F2' AND "users"."deleted_at" IS NULL ORDER BY "users"."id" LIMIT 1.
However, it says it found 0 rows when there is one. If I run the same query in pgadmin, I get a result.
Lucas Pinto
@luckspt
nevermind. user already had data and making a new var for the query fixed it
Evert Arends
@Evert-Arends
Hello everyone. Anyone knows if the documentation of this library is still relevant?
I have hit my toes many, many times. We're trying to create a rest API with go and gorm, but damn this documentation doesn't seem to work for me
I'm talking about gorm.io/gorm v1.21.10
Cascade does not seem to work (not a soft delete)
type Category struct {
    gorm.Model  `json:"gorm_model"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    ParentID    int     `json:"parent_id"`
    Parent      *Category `gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}