Skip to content

Emit ORM-specific database tags #658

Closed

Description

@kyleconroy

#656 added support for adding "db:" tags to generated model structs. The db tag is used by sqlx and gorp. GORM takes a similar approach, but uses the "gorm:" tag.

By design, the emit_db_tags option generates the smallest amount of useful tag information. However, if a user is looking to migrate from gorp / sqlx / GORM, it maybe helpful to generate richer tag information.

For sqlx, I don't think there's anything else the db tag can do. For gorp, this would involve setting attribute like primary key, autoincrement, and size:

type Post struct {
    // db tag lets you specify the column name if it differs from the struct field
    Id      int64  `db:"post_id"`
    Created int64
    Title   string `db:",size:50"`               // Column size set to 50
    Body    string `db:"article_body,size:1024"` // Set both column name and size
}


// Example of using tags to alias fields to column names
// The 'db' value is the column name
//
// A hyphen will cause gorp to skip this field, similar to the
// Go json package.
//
// This is equivalent to using the ColMap methods:
//
//   table := dbmap.AddTableWithName(Product{}, "product")
//   table.ColMap("Id").Rename("product_id")
//   table.ColMap("Price").Rename("unit_price")
//   table.ColMap("IgnoreMe").SetTransient(true)
//
// You can optionally declare the field to be a primary key and/or autoincrement
//
type Product struct {
    Id         int64     `db:"product_id, primarykey, autoincrement"`
    Price      int64     `db:"unit_price"`
    IgnoreMe   string    `db:"-"`
}

GORM has a huge set of options, as the tags are used instead of DDL statements.

There is currently no plan to work on this until we have a specific user that needs this support and is willing to work closely with us on implementation

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    futureIn the year 3000...

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions