GithubHelp home page GithubHelp logo

gorm.io's Introduction

Gorm Official Website

Welcome to the GORM official website, accessible at https://gorm.io.

Contributing to Documentation

At our GitHub repository, we currently only accept pull requests for English documentation. Basic markdown knowledge is sufficient for you to contribute. Feel free to update documents in the pages/docs and pages/gen directories. We highly value your contributions!

The site will automatically deploy new changes to the master branch upon commit.

Translating the Website

For translations into other languages, please visit https://translate.gorm.io. To add a new language for GORM, submit your request via GitHub issues. Approved translations will be managed through the translation site.

Developing this Site

The site is developed using hexo. Start your development with these commands:

$ npm install
$ npm install hexo -g
$ hexo serve

gorm.io's People

Contributors

178inaba avatar 3ks avatar a631807682 avatar arekmano avatar black-06 avatar charleswhchan avatar dchular avatar demomanito avatar denglitong avatar dependabot[bot] avatar dwrss avatar emirb avatar fgottschalk avatar gwillem avatar huangchen007 avatar icemap avatar jinzhu avatar kebhr avatar kohago avatar lpmi-13 avatar openarun avatar qqxhb avatar rafaelhl avatar rsheasby avatar rutsky avatar s-takehana avatar saeidee avatar spankie avatar xxjwxc avatar zaneli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gorm.io's Issues

Go get forces Syntax Errors

github.com/jinzhu/gorm/schema

github.com/jinzhu/gorm/schema

src/github.com/jinzhu/gorm/schema/field.go:271:45: fieldValue.IsZero undefined (type reflect.Value has no field or method IsZero)
src/github.com/jinzhu/gorm/schema/field.go:276:45: fieldValue.IsZero undefined (type reflect.Value has no field or method IsZero)
src/github.com/jinzhu/gorm/schema/field.go:297:27: v.IsZero undefined (type reflect.Value has no field or method IsZero)
src/github.com/jinzhu/gorm/schema/naming.go:95:34: undefined: strings.Builder

While using a go get inside my project I am experiences this new errors right now. This happened from one build where it was working to the next. Didn't change go version or anything belonging to this.

[Question] In the new version, should we be using primary_key or primaryKey

Document Link

Your Question

We use gorm to manage our MySQL based tables. We recently upgraded to new gorm version and noticed "unique_index" was not working. We fixed our code to change it to "uniqueIndex". At the same time, we noticed that "primary_key" was still working for our tables where we use composite primary keys.

Expected answer

Please let us know is this expected, working accidentally and/or will be deprecated towards "primaryKey" in future?

Inefficient db writes when creating multiple child records at once

Creating multiple "Belongs To" children records at the same time as creating the parent should execute 1 INSERT statement for all of the child records.

From the MySQL manual

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

how to translate for v2 doc ?

Your Question

I'd like to do a Japanese translation of the v2 documentation site.
What is the best way to contribute?

Expected answer

pull request
or crowdin
or Should I wait a bit?

[Question] Would it be helpful to add a warning note about using prepared statements?

Document Link

`PreparedStmt` creates prepared statement when executing any SQL and caches them to speed up future calls, for example:

Your Question

Should there be a warning that prepared statements don't apply to all use cases? Reading the documentation it reads like it's always a good idea to use prepared statements for improved speed.

There's a slight issue to be concerned with when using connection poolers like PGBouncer.

Useful article: https://www.depesz.com/2012/12/02/what-is-the-point-of-bouncing/

TLDR: It's possible that statements prepared on one database connection aren't available on another. When using a connection pooler the prepare / query execution may use different connections.

I realized this issue when writing tinypg. Here's the changelog description: https://github.com/joeandaverde/tinypg/blob/ab648e650298c68974be8a8fc827cc223795f563/CHANGELOG.md#L17-L23. I hope those learnings are helpful to this project!

Expected answer

Was this helpful?

How I can handle tables Has One to a table [Question]

Document Link

https://gorm.io/docs/has_one.html#Self-Referential-Has-One

Your Question

Following the document, we have a simple case for has-one relationship like below

// User has one CreditCard, CreditCardID is the foreign key
type User struct {
  gorm.Model
  CreditCard CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}

I'm starting concern what if we have 2 tables contain has-one relationship like

type Teacher struct {
  gorm.Model
  CreditCard CreditCard
}

type Student struct {
  gorm.Model
  CreditCard CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  TeacherId uint
  StudentId uint
}

Let's say each time we create a teacher or a student, we also need to create their credit cards. So with current implementation, how we can solve it? Thank you.

Expected answer

Incorrect HAS_ONE example

The example in this new documentation appears to be incorrect, unlike the one in the older version, as explained on https://github.com/jinzhu/gorm/issues/2206 : in short, unlike the BELONGS_TO case, with HAS_ONE (like HAS_MANY), the FK ID is on the owned entity, not on the owner.

In the User/CreditCard example, this means having UserID on CreditCard.

Polish translation

Hey, if we could set up Polish translation on gorm.io, I'd be happy to help!

Add Yoruba translation

I would like to partake in translation to Yoruba, one of the major Nigerian languages. Thanks

French translation

Hey, I would be interested in starting working on a French translation.

[New Translation] Spanish associations.md

Request New Translation

  • Language Name: Spanish
  • Language Code: es-ES
  • Disqus Lang Code: es

Please only request to translate a new language if you are a GORM & Golang user at least one year, and can translate at least 40% of documents

Thank you.

German Translation

Hi, I love your gorm project and would be happy to help by translating your docs to german!

Arabic translation

Hello , i'll be very happy if you allow me to help to translate the gorm documentation in Arabic

Clarify please the role of "Not null" for required fields

http://gorm.io/docs/models.html

type User struct {
  gorm.Model
  Name         string
  Age          sql.NullInt64
  Birthday     *time.Time
  Email        string  `gorm:"type:varchar(100);unique_index"`
  Role         string  `gorm:"size:255"` // set field size to 255
  MemberNumber *string `gorm:"unique;not null"` // set member number to unique and not null
  Num          int     `gorm:"AUTO_INCREMENT"` // set num to auto incrementable
  Address      string  `gorm:"index:addr"` // create index with name `addr` for address
  IgnoreMe     int     `gorm:"-"` // ignore this field
}
CREATE TABLE public.users
(
  id		serial,
  created_at	timestamp with time zone,
  updated_at	timestamp with time zone,
  deleted_at	timestamp with time zone,
  name		text,
  age		integer,
  birthday	timestamp with time zone,
  email		varchar(100) unique,
  role		varchar(255),
  member_number	text unique not null,
  num		integer,
  address	text,
  CONSTRAINT users_pkey PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

The point here is: why the migration to SQL is not null if the column is required?
Just take a look to Age. This field de-facto is required, so why the migration isn't adding the clausule not null in the sql table definition?

And finally: MemberNumber *string looks really strange to me. That field de-facto accepts null values, but the label gorm:not null contradicts that definition. Why?

Line breaking for long query example so that user do not need to scroll horizontally and text overlapped

From:

db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)
//// SELECT * from USERS WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (users)
//// SELECT count(*) FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (count)
db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count)
//// SELECT count(*) FROM users WHERE name = 'jinzhu'; (count)
db.Table("deleted_users").Count(&count)
//// SELECT count(*) FROM deleted_users;

into:

db.
     Where("name = ?", "jinzhu").
     Or("name = ?", "jinzhu 2").
     Find(&users).
     Count(&count)

// SELECT *
// FROM USERS
// WHERE
//     name = 'jinzhu' OR
//     name = 'jinzhu 2'
// ; (count)

// SELECT count(*)
// FROM users
// WHERE
//     name = 'jinzhu' OR
//     name = 'jinzhu 2'
// ; (count)

db.
     Model(&User{}).
     Where("name = ?", "jinzhu").
     Count(&count)
// SELECT count(*)
// FROM users
// WHERE name = 'jinzhu'; (count)

db.
     Table("deleted_users").
     Count(&count)
// SELECT count(*)
// FROM deleted_users;

[New Translation] Portuguese Brazil

Request New Translation

  • Language Name: Portuguese Brazil
  • Language Code: pt-BR
  • Disqus Lang Code: pt

I'm using GORM and Golang +-2 years on most projects.

Please only request to translate a new language if you are a GORM & Golang user at least one year, and can translate at least 40% of documents

Thank you.

[Question] Joins-Preloading explanation of generated join type unclear.

Document Link

https://gorm.io/docs/preload.html#Joins-Preloading

Your Question

The documentation states that "Join Preload will loads association data using inner join". But the (Company) query example shows a conditional left join being generated and not an inner join. Its also not clear if this is always the case when using Joins() or only when using Joins() in combination with extra conditions.

Expected answer

Clear explanation of which type of join is used when.

Full Composite Keys are not used when retrieving using Related

This statement:
db.First(&term, edu.Term{CompanyID: companyID, SchoolID: schoolID, ID: id}).Related(&term.School)

Produces the following SQL
[2018-08-30 16:03:06] [0.53ms] SELECT * FROM local-edu-terms WHERE local-edu-terms.deleted_at IS NULL AND ((local-edu-terms.company_id = '2ivyBYA34mJF64xZydUfoC') AND (local-edu-terms.school_id = '3421SZLzgHw3hjM7DEfioa') AND (local-edu-terms.id = '2GvoEzV6n2noEOyyqOlwf4')) ORDER BY local-edu-terms.id ASC LIMIT 1
[1 rows affected or returned ]

[2018-08-30 16:03:06] [0.49ms] SELECT * FROM local-edu-schools WHERE local-edu-schools.deleted_at IS NULL AND ((id = '3421SZLzgHw3hjM7DEfioa')) ORDER BY local-edu-schools.id ASC
[1 rows affected or returned ]

In the above example, local-edu-terms is a child of(Belongs To) local-edu-schools
The second SQL statement should be:
SELECT * FROM local-edu-schools WHERE local-edu-schools.deleted_at IS NULL AND ((local-edu-schools.company_id = '2ivyBYA34mJF64xZydUfoC') AND (id = '3421SZLzgHw3hjM7DEfioa')) ORDER BY local-edu-schools.id ASC

Automigrate not working with dbresolver

Trying to use two databases master and shard1.

Tried using dbresolver to automigrate some models for both databases.

Here is the code for it

dsnMaster := viper.GetString("MYSQL_DSN_MASTER")
dsnShard1 := viper.GetString("MYSQL_DSN_SHARD1")

db, err := gorm.Open(mysql.Open(dsnMaster), &gorm.Config{
	Logger: logger.Default.LogMode(logger.Info)})

if err != nil {
	log.Println("Connection Failed to Open")
} else {
	log.Println("Connection Established")
}

db.Use(dbresolver.Register(dbresolver.Config{
	Sources: []gorm.Dialector{mysql.Open(dsnShard1)},
}, &models.WorkspaceGroup{}, &models.GroupMember{}, &models.WorkspaceFile{}, &models.WorkspacePermission{}, &models.Permission{}, &models.WorkspacePermissionEntry{}, "shard1"))

db.AutoMigrate(&models.Workspace{}, &models.WorkspaceMember{})

// db.AutoMigrate(&models.Workspace{}, &models.WorkspaceMember{}, &models.WorkspaceGroup{}, &models.GroupMember{})

db.Clauses(dbresolver.Use("shard1")).AutoMigrate(&models.WorkspaceGroup{}, &models.GroupMember{}, &models.WorkspaceFile{}, &models.WorkspacePermission{}, &models.Permission{}, &models.WorkspacePermissionEntry{})

When trying to automigrate on models for shard1, it is not getting reflected in the database. Changes are still going to master database. No error is thrown, dbresolver is not able to resolve correct database during automigration.

It there anything that needs to be done? Or is it an issue?

[Question] About New Session Mode

Document Link

Method Chaining > New Session Mode

Your Question

The two examples are very confusing. Correct me if I'm wrong:

In example 2, the last query tx.Where("age = ?", 28).Find(&users) is incorrectly sharing the where clauses of an already-executed query (tx.Where("age = ?", 18).Find(&users)), because the two queries were derived from the same session (created by tx := db.Where("name = ?", "jinzhu")), and because the finisher Find() does not reset internal state of tx.

However, in example 1, all the three queries were also derived from the same db which has the same type (*gorm.DB) as tx in example 2. And since finishers does not reset internal state of tx, one would reasonably think it behaves the same for db. So, after the first query db.Where("name = ?", "jinzhu").Where("age = ?", 18).Find(&users), db is no longer a "newly initialized *gorm.DB" (since a query has been executed on it). If that is true, how come the second query db.Where("name = ?", "jinzhu2").Where("age = ?", 20).Find(&users) does not share the where statement from the first query?

I can think of two explanations:

  1. Either: finishers would reset internal state of db but not tx.
  2. Or: db is still considered a "newly initialized *gorm.DB", no matter how many queries it has ran.

I kinda think explanation 2 is the correct one, but I'm no sure. Would you mind to clarify? I saw there's another user asking the same question (essentially) in comments of the documentation page, but so far nobody really answered.

Expected answer

[Feature] Dark theme

Describe the feature

Adding a dark theme with the ability to switch to a light one. By default, the selected system theme is expected

Motivation

I, like many other programmers, use a dark theme in development and surfing. I think that many people will be grateful to read the documentation in a convenient environment for them


UPD: I can help with this if there is any help with the design or other help :)

U need an Italian translation?

Hi, some of the title? if you need you can add in crowdin page and give me the proof of reader abilitation (some nickname "Deusjudo"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.