gorm: AutoMigrate failed (type "bigserial" does not exist)

GORM Playground Link

https://github.com/go-gorm/playground/pull/391

Existing database without using GORM :

DROP TABLE IF EXISTS "events";
DROP SEQUENCE IF EXISTS events_id_seq;
CREATE SEQUENCE events_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."events" (
    "id" integer DEFAULT nextval('events_id_seq') NOT NULL,
    "town" text NOT NULL,
    "latitude" real NOT NULL,
    "longitude" real NOT NULL,
    "kind" text NOT NULL,
    "species" text NOT NULL,
    "description" text NOT NULL,
    "user_publisher" integer NOT NULL,
    "time" timestamp NOT NULL,
    "disabled" integer NOT NULL,
    "report" integer NOT NULL,
    CONSTRAINT "events_pkey" PRIMARY KEY ("id")
) WITH (oids = false);

Model used with GORM :

type Event struct {
	gorm.Model
	Town           string    `gorm:"not null"`
	Latitude       float64   `gorm:"not null"`
	Longitude      float64   `gorm:"not null"`
	Kind           string    `gorm:"not null"`
	Species        string    `gorm:"not null"`
	Description    string    `gorm:"not null"`
	User_Publisher int       `gorm:"not null"`
	Time           time.Time `gorm:"not null"`
	Disabled       int       `gorm:"not null"`
	Report         int       `gorm:"not null"`
}

Function that opens the database and migrates the data :

func (store PostgresRepository) Open(dsn string) (*PostgresRepository, error) {
	// Open database
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		return nil, err
	}
	log.Info().Msg("Connected to db")

	// Create the new SQLite repository
	sql := NewPostgresRepository(db)

	// Migration database
	if err := db.AutoMigrate(&Event{}); err != nil {
		return nil, err
	}

	return sql, nil
}

Error : 2021/10/19 15:14:19 /home/enzo/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:252 ERROR: type “bigserial” does not exist (SQLSTATE 42704)

Description

Before using GORM I was using pgxscan and doing automatic migrations via a script. Recently I switched to GORM which is more efficient but I get this error during the migration.

Why do I get this error?

Same problem here #4765

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Hello, any news about this issue ?