LoginSignup
0
0

More than 3 years have passed since last update.

gobuffaloのテーブル名にはデフォルトで"s"がつく前提になっている

Posted at

結論から

  • gobuffaloで使うテーブル名には末尾に"s"がいる。
  • 例を言うと「商品」を格納する構造体「product」を実装して、テーブルに格納する場合、テーブル名は「products」である必要ある。※デフォルトでは。

テーブル名を自分の好きにカスタマイズする方法

  • 該当のmodel.goでTableName 関数をオーバーライドする
  • 以下は例です。
package models

import (
    "encoding/json"
    "time"

    "github.com/gobuffalo/pop"
    "github.com/gobuffalo/validate"
    "github.com/gofrs/uuid"
)

// Test is used by pop to map your .model.Name.Proper.Pluralize.Underscore database table to your go code.
type Test struct {
    ID        uuid.UUID `json:"id" db:"id"`
    Messsage  string    `json:"messsage" db:"messsage"`
    CreatedAt time.Time `json:"created_at" db:"created_at"`
    UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

// ★★★ここでテーブル名を指定することが出来ます。
func (t Test) TableName() string {
  return "testTest"
}

// String is not required by pop and may be deleted
func (t Test) String() string {
    jt, _ := json.Marshal(t)
    return string(jt)
}

// Tests is not required by pop and may be deleted
type Tests []Test

// String is not required by pop and may be deleted
func (t Tests) String() string {
    jt, _ := json.Marshal(t)
    return string(jt)
}

// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (t *Test) Validate(tx *pop.Connection) (*validate.Errors, error) {
    return validate.NewErrors(), nil
}

// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
// This method is not required and may be deleted.
func (t *Test) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
    return validate.NewErrors(), nil
}

// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
// This method is not required and may be deleted.
func (t *Test) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
    return validate.NewErrors(), nil
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0