1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rust】SeaORMのmigrationでPostgreSQLの配列型を設定する

Posted at

概要

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?;
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?