GORM Playground Link

(no playground link)

Description

Postgres has a max identifier length of 63 bytes. I see a test here that's asserting for 64 characters/bytes.

https://github.com/go-gorm/gorm/blob/master/schema/naming_test.go#L196

https://til.hashrocket.com/posts/8f87c65a0a-postgresqls-max-identifier-length-is-63-bytes

https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: ghostsquad

Not stale. I don't think that a playground PR is strictly necessary here. Links to the documentation and current tested expectations have been mentioned.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: ghostsquad

Insistent little bot...

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

Since different databases support different lengths, we can add configuration items to schema.NamingStrategy and change the default configuration of Postgres driver. Contributions welcome, let me know if anyone is interested.

Comment From: alidevhere

Since different databases support different lengths, we can add configuration items to schema.NamingStrategy and change the default configuration of Postgres driver. Contributions welcome, let me know if anyone is interested.

@a631807682

Hii !! Please assign this issue to me, i would love to work on it.

Comment From: alidevhere

@a631807682 Sorry if i sound like a fool, but this is my first contribution in this repo. And i still don't get how can we do that? Let me explain my self what i mean

You asked to add configuration item in schema.NamingStrategy So i added NamingStrategyConfig see here :

// NamingStrategy tables, columns naming strategy
type NamingStrategy struct {
    TablePrefix   string
    SingularTable bool
    NameReplacer  Replacer
    NoLowerCase   bool
    NamingStrategyConfig
}

// This struct is used to configure the behavior NamingStrategy, according to DB.
// For example, in MySQL, the maximum length of an identifier is 64 characters.
// In PostgreSQL, the maximum length of an identifier is 63 characters.
// In SQL Server, the maximum length of an identifier is 128 characters.
// In SQLite, the maximum length of an identifier is unlimited.
// In future, we may add more options to NamingStrategyConfig.
type NamingStrategyConfig struct {
    IdentifierMaxLength int
}

Since i have added IdentifierLength field in NamingStrategy so i had impression we would use schema.NamingStrategy to populate Identifier Length, like this

if config.NamingStrategy == nil {
        // Set default value of IdentifierMaxLength according to the database type
        identifierMaxLength := 64
        switch dialector.Name() {
        case "mysql":
            identifierMaxLength = 64
        case "postgres":
            identifierMaxLength = 63
        case "sqlite":
            identifierMaxLength = 64
        case "sqlserver":
            identifierMaxLength = 128
        }

        config.NamingStrategy = schema.NamingStrategy{NamingStrategyConfig: schema.NamingStrategyConfig{IdentifierMaxLength: identifierMaxLength}}
    }

You said its not recommended to do it on the basis of dialector name. ok got it !! 👍🏻
And you told me that We can use Apply()

Now my question is: how can we pass identifier length to Apply according to choosen dialector ?? (because apparently driver implementing interface dialector is passed, and we need to pass Identifeir Lengths to NamingStrategy according to DB)

How we know we need to initialize identifierMaxLength as 63,64 or 128 if we don't kknow the DB.

if d, ok := dialector.(interface{ Apply(*Config) error }); ok {
        if err = d.Apply(config); err != nil {
            return
        }
    }

I know you are busy, but your little help will push me to complete this task, Thanks 😄

Comment From: a631807682

Now my question is: how can we pass identifier length to Apply according to choosen dialector ?? (because apparently driver implementing interface dialector is passed, and we need to pass Identifeir Lengths to NamingStrategy according to DB)

How we know we need to initialize identifierMaxLength as 63,64 or 128 if we don't kknow the DB.

We just need to set a default value, and then override it through Apply api in each driver

Comment From: myusko

@a631807682 Does it make sense to close the issue? since it was addressed in the https://github.com/go-gorm/gorm/pull/6337