8000 AutoMigrate doesn't fully handle autoincrement for postgres · Issue #1159 · uptrace/bun · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
AutoMigrate doesn't fully handle autoincrement for postgres #1159
Closed
@bgrant0607

Description

@bgrant0607

This is to make you aware of the issue. I don't have a full fix yet.

autoincrement fields map to serial (for int) and bigserial (for int64) pseudo-types.

serial and bigserial are kind of like macros that expand into integer and bigint.

https://www.postgresql.org/docs/9.1/datatype-numeric.html

"The data types serial and bigserial are not true types, but merely a notational convenience
for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases)."

int64 autoincrement is generated as bigserial, which in postgres automatically translates to:

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
	colname bigint DEFAULT nextval('tablename_colname_seq')::regclass NOT NULL
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

This interferes with the diff code in multiple ways:

  1. Changes the type
  2. Adds NOT NULL
  3. Adds a DEFAULT

I'm not sure whether the diff code pays attention to the ALTER SEQUENCE yet.

https://github.com/uptrace/bun/blob/master/migrate/diff.go#L277

func (d detector) equalColumns(col1, col2 sqlschema.Column) bool {
	return d.cmpType(col1, col2) &&
		col1.GetDefaultValue() == col2.GetDefaultValue() &&
		col1.GetIsNullable() == col2.GetIsNullable() &&
		col1.GetIsAutoIncrement() == col2.GetIsAutoIncrement() &&
		col1.GetIsIdentity() == col2.GetIsIdentity()
}

The comparison function passed for postgres doesn't tolerate the type change.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0