モデルを宣言する
モデルは Goの基本型、(基本型の)ポインタ/エイリアス、 Scanner および Valuer インターフェイスを実装するカスタム型からなる通常の構造体です。
例:
type User struct { |
規約
GORM prefers convention over configuration. By default, GORM uses ID
as primary key, pluralizes struct name to snake_cases
as table name, snake_case
as column name, and uses CreatedAt
, UpdatedAt
to track creating/updating time
If you follow the conventions adopted by GORM, you’ll need to write very little configuration/code. If convention doesn’t match your requirements, GORM allows you to configure them
gorm.Model
GORMは gorm.Model
構造体を定義しています。これには ID
, CreatedAt
, UpdatedAt
, DeletedAt
のフィールドが含まれます。
// gorm.Modelの定義 |
この構造体を埋め込むことで、これらのフィールドを自身の構造体に含めることができます。 Embedded Struct も参照してください。
高度な機能
フィールドレベルの権限
Exported fields have all permissions when doing CRUD with GORM, and GORM allows you to change the field-level permission with tag, so you can make a field to be read-only, write-only, create-only, update-only or ignored
注意 GORM Migrator を使用してテーブルを作成した場合、除外設定されたフィールドは作成されません
type User struct { |
作成・更新日時のトラッキング/Unix (ミリ・ナノ) 秒でのトラッキング
GORMの規約では、作成/更新時間をトラッキングするのに CreatedAt
, UpdatedAt
を使用します。それらのフィールドがモデルに定義されている場合、作成/更新時間に現在時刻を値としてセットします。
別の名前のフィールドを使用する場合、 autoCreateTime
、 autoUpdateTime
タグを使用することで設定を変更することができます。
time.Timeの代わりにUNIX (ミリ/ナノ) 秒を保存したい場合、フィールドのデータ型を time.Time
から int
に変更するだけで保存が可能になります。
type User struct { |
構造体の埋め込み
匿名(anonymous field)フィールドでモデルの定義がなされている場合、埋め込まれた構造体のフィールドは親の構造体のフィールドとして含まれることになります。例:
type User struct { |
通常のフィールドで構造体の定義がなされている場合、 embedded
タグを使用して構造体の埋め込みを行うことができます。例:
type Author struct { |
また、 embeddedPrefix
タグを使用することで、埋め込まれた構造体のフィールド名にプレフィックスを追加することができます。例:
type Blog struct { |
タグはモデル宣言時に任意で使用できます。GORMは以下のタグをサポートしています。(タグは大文字小文字を区別しませんが、 camelCase
が推奨されます)
タグ名 | 説明 |
---|---|
column | データベースのカラム名 |
type | カラムのデータ型を指定します。bool, int, uint, float, string, time, bytes などの全てのデータベースで動作する一般的なデータ型と互換性のあるものが良いでしょう。また、not null , size , autoIncrement … などの他のタグと併用することも可能です。 varbinary(8) などの特定のデータベースでのみ使用可能なデータ型もサポートしていますが、それらのデータ型を使用する場合は、データ型をフルで指定する必要があります。例: MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT |
serializer | specifies serializer for how to serialize and deserialize data into db, e.g: serializer:json/gob/unixtime |
size | specifies column data size/length, e.g: size:256 |
primaryKey | 主キーとなるカラムを指定します |
unique | specifies column as unique |
default | カラムのデフォルト値を指定します |
precision | specifies column precision |
scale | specifies column scale |
not null | カラムをNOT NULLで指定します |
autoIncrement | specifies column auto incrementable |
autoIncrementIncrement | auto increment step, controls the interval between successive column values |
embedded | embed the field |
embeddedPrefix | column name prefix for embedded fields |
autoCreateTime | track current time when creating, for int fields, it will track unix seconds, use value nano /milli to track unix nano/milli seconds, e.g: autoCreateTime:nano |
autoUpdateTime | track current time when creating/updating, for int fields, it will track unix seconds, use value nano /milli to track unix nano/milli seconds, e.g: autoUpdateTime:milli |
index | create index with options, use same name for multiple fields creates composite indexes, refer Indexes for details |
uniqueIndex | same as index , but create uniqued index |
check | creates check constraint, eg: check:age > 13 , refer Constraints |
<- | set field’s write permission, <-:create create-only field, <-:update update-only field, <-:false no write permission, <- create and update permission |
-> | set field’s read permission, ->:false no read permission |
- | ignore this field, - no read/write permission, -:migration no migrate permission, -:all no read/write/migrate permission |
comment | add comment for field when migration |
アソシエーションで使用できるタグ
GORMではアソシエーション用のタグを使用することで、外部キー、制約、many2many(多対多)テーブルなどを設定できます。詳細は Associations section を参照してください。