Created
April 25, 2020 10:59
-
-
Save mangalaman93/ea956abea624e045035f42395acc969f to your computer and use it in GitHub Desktop.
Use gorm with schema in postgres database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/postgres" | |
) | |
type User struct { | |
ID int | |
Username string | |
} | |
func main() { | |
db, err := gorm.Open("postgres", "host=localhost port=5432 sslmode=disable dbname=aman") | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
db.LogMode(true) | |
// set schema here. | |
gorm.DefaultTableNameHandler = func(db *gorm.DB, tableName string) string { | |
return "s1." + tableName | |
} | |
db.CreateTable(&User{}) | |
} |
how to use in v2?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!