Created
August 24, 2020 14:28
-
-
Save mutuadavid93/3ca3870d13d76443057e9b6e740848cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"fmt" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/postgres" | |
) | |
type User struct { | |
// Embed Model struct promoting it's fields to User struct | |
gorm.Model | |
// Other User specific fields | |
Name string | |
Email *string `gorm:"unique_index;not null"` | |
} | |
// Reference :: https://pkg.go.dev/github.com/lib/pq?tab=doc | |
// Create all PostgreSQL Database Configs | |
const ( | |
host = "localhost" | |
port = 5432 | |
dbname = "lenslocked_dev" | |
user = "streetmoney" | |
) | |
func main() { | |
driverName := "postgres" | |
// Note :: Remove the password database field in the psqlinfo below if it's empty | |
// otherwise psql will behave abnormally | |
psqlinfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", | |
host, port, user, dbname) | |
// Note :: sql.OPen() doesn't connect to database, it only ensures the | |
// driver and datasource are valid | |
db, err := gorm.Open(driverName, psqlinfo) | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
// db.DropTableIfExists(&User{}) | |
// | |
// Migrate the schema creating the User table for us | |
// db.AutoMigrate(&User{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment