Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用Postgresql生成的代码运行报错 #21

Open
yb123speed opened this issue Sep 7, 2020 · 0 comments
Open

使用Postgresql生成的代码运行报错 #21

yb123speed opened this issue Sep 7, 2020 · 0 comments

Comments

@yb123speed
Copy link

yb123speed commented Sep 7, 2020

func createDatabase() (*gorm.DB, error) {
	dbType := config.GetString("db.type")
	dbAddr := config.GetString("db.addr")
	dbName := config.GetString("db.database")
	dbUser := config.GetString("db.user")
	dbPassword := config.GetString("db.password")
	dbCharset := config.GetString("db.charset")
	conn := ""
	switch dbType {
	case "mysql":
		conn = fmt.Sprintf("%s:%s@(%s)/%s?charset=%s&parseTime=True&loc=Local", dbUser, dbPassword, dbAddr, dbName, dbCharset)
	case "sqlite":
		conn = dbAddr
	case "mssql":
		return nil, errors.New("TODO:suport sqlServer")
	case "postgres":
		hostPort := strings.Split(dbAddr, ":")
		if len(hostPort) == 2 {
			return nil, errors.New("db.addr must be like this host:ip")
		}
		conn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", hostPort[0], hostPort[1], dbUser, dbName, dbPassword)
	default:
		return nil, fmt.Errorf("database type %s is not supported by felix ginrbo", dbType)
	}
	return gorm.Open(dbType, conn)
}

其中的

if len(hostPort) == 2 {
			return nil, errors.New("db.addr must be like this host:ip")
		}

应该是len(hostPort) != 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant