概要
RustのORMライブラリである「SeaORM」には、migrationの機能があります。migrationのファイルはRustでDBマイグレーションの仕組みを導入(SeaORM)の記事で紹介されている通り、rustのコードで実装する感じになります。
今回はこのmigrationの実装で、PostgreSQLの配列型を設定したい場合にどうするかのメモ書きです。
前提
- 使用したSeaORMのバージョンは
1.1.16です。
対応方針
ドキュメントのColumnTypeを参照すると、Array型が用意されているのでこれを使用してみます。
実装サンプル
まずは以下のようなモデルを用意します。
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "sample_settings")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: String,
pub name: String,
pub attributes: Vec<String>,
pub description: Option<String>,
}
attributesのカラムを配列型としてmigrationします。今回は文字列の配列なので、arrayメソッドの中でString型を設定します。
manager
.create_table(
Table::create()
.table(sample_settings::Entity)
.if_not_exists()
.col(
ColumnDef::new(sample_settings::Column::Id)
.string()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(sample_settings::Column::Name)
.string()
.not_null(),
)
.col(
ColumnDef::new(sample_settings::Column::Attributes)
.array(ColumnType::String(StringLen::default())),
)
.col(ColumnDef::new(sample_settings::Column::Description).text())
.to_owned(),
)
.await?;