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

Column Ordering Changed after v1.2.0 for Embedded Structs #1095

Closed
hhuseyinpay opened this issue Jan 1, 2025 · 3 comments · Fixed by #1098
Closed

Column Ordering Changed after v1.2.0 for Embedded Structs #1095

hhuseyinpay opened this issue Jan 1, 2025 · 3 comments · Fixed by #1098

Comments

@hhuseyinpay
Copy link

Description

After upgrading to bun v1.2.0, the column ordering in generated SQL tables has changed when using embedded structs. Previously, columns were created in the order they were defined in the struct (including embedded structs). However, in v1.2.0, the columns from embedded structs appear after the directly defined columns, regardless of their position in the struct definition.

Reproduction

// 20210505110026_foo.go 
package main
type BaseModel struct {
	ID        int
	CreatedAt time.Time `bun:",default:current_timestamp"`
}
type FooModel struct {
	BaseModel

	Name string
}

func init() {
	Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
		_, err := db.NewCreateTable().Model((*FooModel)(nil)).Exec(ctx)
		return err
	}, func(ctx context.Context, db *bun.DB) error {
		_, err := db.NewDropTable().Model((*FooModel)(nil)).Exec(ctx)
		return err
	})
}
// main.go
package main
import (
	"context"
	"database/sql"

	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/sqlitedialect"
	"github.com/uptrace/bun/driver/sqliteshim"
	"github.com/uptrace/bun/migrate"
)

var Migrations = migrate.NewMigrations()

func main() {
	sqldb, err := sql.Open(sqliteshim.ShimName, "file:test.s3db?cache=shared")
	if err != nil {
		panic(err)
	}

	db := bun.NewDB(sqldb, sqlitedialect.New())
	if err := Migrations.DiscoverCaller(); err != nil {
		panic(err)
	}
	migrator := migrate.NewMigrator(db, Migrations)

	ctx := context.Background()
	err = migrator.Init(ctx)
	if err != nil {
		panic(err)
	}

	_, err = migrator.Migrate(ctx)
	if err != nil {
		panic(err)
	}
}

Expected Behavior

The generated table should have columns in the order they are defined in the struct hierarchy:

  1. id
  2. created_at
  3. name

Actual Behavior

After v1.2.0, the generated table has columns in this order:

  1. name
  2. id
  3. created_at

The columns from the embedded BaseModel struct are placed after the directly defined Name column, which breaks the expected structural order.

Version Information

  • Before: Working as expected in versions prior to v1.2.0
  • After: Column ordering changed in v1.2.0 and above

Impact

This change affects systems that rely on consistent column ordering, particularly in scenarios where:

  • Database tools or ORMs assume a specific column order
  • Migration scripts depend on predictable column positioning
  • Legacy system integrations expect a particular column sequence

Additional Notes

The change in column ordering appears to be related to how bun v1.2.0 handles embedded struct fields during table creation. While this doesn't affect functionality, it represents a breaking change in terms of database schema generation.

@j2gg0s
Copy link
Collaborator

j2gg0s commented Jan 1, 2025

Yes, your judgment is correct; it was caused by this 9052fc4c.

I’m sorry for not maintaining full compatibility during this process.

@hhuseyinpay
Copy link
Author

Thank you for confirming the cause. Could you clarify if this issue is planned to be resolved? If so, is there an estimated timeline for the fix?

@j2gg0s
Copy link
Collaborator

j2gg0s commented Jan 2, 2025

Thank you for confirming the cause. Could you clarify if this issue is planned to be resolved? If so, is there an estimated timeline for the fix?

@hhuseyinpay

If necessary, we can add a flag to control whether to maintain the previous order.
Do you have any scenarios where this is being blocked?

j2gg0s added a commit that referenced this issue Jan 2, 2025
@j2gg0s j2gg0s closed this as completed in 5edb672 Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants