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

Postgres Max Identifier length is 63 bytes #6098

Closed
ghostsquad opened this issue Feb 23, 2023 · 10 comments
Closed

Postgres Max Identifier length is 63 bytes #6098

ghostsquad opened this issue Feb 23, 2023 · 10 comments
Assignees

Comments

@ghostsquad
Copy link

ghostsquad commented Feb 23, 2023

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

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Feb 23, 2023
@github-actions
Copy link

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.ioSearch Before Asking

@ghostsquad
Copy link
Author

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.

@github-actions
Copy link

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.ioSearch Before Asking

@ghostsquad
Copy link
Author

Insistent little bot...

@github-actions
Copy link

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.ioSearch Before Asking

@a631807682
Copy link
Member

a631807682 commented Mar 4, 2023

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.

@alidevhere
Copy link
Contributor

alidevhere commented Mar 16, 2023

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.

@a631807682 a631807682 assigned alidevhere and unassigned jinzhu Mar 17, 2023
alidevhere added a commit to alidevhere/gorm that referenced this issue Mar 28, 2023
@alidevhere
Copy link
Contributor

@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 😄

@a631807682
Copy link
Member

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

@myusko
Copy link

myusko commented Feb 25, 2024

@a631807682 Does it make sense to close the issue? since it was addressed in the #6337

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

No branches or pull requests

5 participants