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

gorm automigrator fails with postgres when a view exists despite no updates to struct in migration #5625

Closed
jawad-unmarshal opened this issue Aug 19, 2022 · 3 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@jawad-unmarshal
Copy link

GORM Playground Link

go-gorm/playground#508

Description

Scenario:

  1. Migrate table (Make sure the struct has some go type of float64 data.
  2. Create a View
  3. Re-Run Migrations

Expected Result:

All steps complete successfully

Result Achieved

The migrations in step 3 fail.

ERROR: cannot alter type of a column used by a view or rule (SQLSTATE 0A000)

The alteration shouldn't be necessary given the struct hasn't changed at all.

In one case I found out this was because it was converting a column that was numeric to decimal and failing to do so.

steps to get around:

The tests will pass if you specify a particular postgres type to the data like so:

type User struct {
	gorm.Model
	Name      string
	Age       uint
	Birthday  *time.Time
	Account   Account
	Pets      []*Pet
	Toys      []Toy `gorm:"polymorphic:Owner"`
	CompanyID *int
	Company   Company
	ManagerID *uint
	Manager   *User
	Team      []User     `gorm:"foreignkey:ManagerID"`
	Languages []Language `gorm:"many2many:UserSpeak"`
	Friends   []*User    `gorm:"many2many:user_friends"`
	Active    bool
	TestValue float64 `gorm:"type:numeric"`
}
@emilhakobian
Copy link

Any updates on this issue? I experience the same problem. After recent update to the latest version of gorm.

@emilhakobian
Copy link

Solved when describing aliases explicitly.
-- pgsql
smallint <-> int2
integer <-> int4
bigint <-> int8
decimal <-> numeric

Source: #5627 (comment)

@a631807682
Copy link
Member

#5627

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

No branches or pull requests

4 participants