[Rust]SeaORMで基本的なSelect操作を行う方法

Rust

はじめに

今回は、SeaORMのselect処理において、頻繁に使用するような処理を紹介します。

Select | SeaORM 🐚 An async & dynamic ORM for Rust
Once you have defined the entity, you are ready to retrieve data from the database. Each row of data...

なお、SeaORMの導入方法については、こちらを参考にしてください

Rust×SeaORMで学ぶORM入門:セットアップと基本操作を解説
はじめに今回は、Rustの人気ORMパッケージであるSeaORMをインストールから、簡単なデータベース操作までを解説します。なお、今回紹介するソースコードはこちらにあります。また、Rustのデスクトッ...

テーブルすべてのレコードを取得する

userテーブルのid=1のデータを取得する方法です。


     // userテーブルから1件だけ取得
    let results: Vec = entities::user::Entity::find().all(&db).await?;
    // 取得した投稿のタイトルを表示
    for user in results {
        println!("{:?}", user);
    }
  

取得結果:


Model { id: 1, name: "Alice", email: "..." }
Model { id: 2, name: "Tom", email: "..." }

プライマリーキーから1件取得する

userテーブルのid=1のデータを取得する方法です。


    let result: Option = entities::user::Entity::find_by_id(1).one(&db).await?;
 

条件を指定する

// userテーブルからNameが"Alice"のユーザーを取得
let eq: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Name.eq("Alice"))
    .all(&db)
    .await?;
// userテーブルからNameが"A"を含むユーザーを取得
let contains: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Name.contains("A"))
    .all(&db)
    .await?;
// userテーブルからNameが"Alice"でないユーザーを取得
let not_eq: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Name.ne("Alice"))
    .all(&db)
    .await?;
// userテーブルのIDが2以下のユーザーを取得
let lt: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Id.lt(3))
    .all(&db)
    .await?;
// userテーブルのIDが2以上のユーザーを取得
let gte: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Id.gte(2))
    .all(&db)
    .await?;
// userテーブルのIDが1以上かつ3以下のユーザーを取得
let between: Vec = entities::user::Entity::find()
    .filter(entities::user::Column::Id.between(1, 3))
    .all(&db)
    .await?;
  

昇順降順で並び替える

// Nameカラムが昇順でユーザーを取得
let asc: Vec = entities::user::Entity::find()
    .order_by_asc(entities::user::Column::Name)
    .all(&db)
    .await?;
// Nameカラムが降順でユーザーを取得
let desc: Vec = entities::user::Entity::find()
    .order_by_desc(entities::user::Column::Name)
    .all(&db)
    .await?;
// Nameカラムが昇順かつnullを先頭にしてユーザーを取得
let asc_nulls_first: Vec = entities::user::Entity::find()
    .order_by_with_nulls(entities::user::Column::Name, Order::Asc, NullOrdering::First)
    .all(&db)
    .await?;
// Nameカラムが降順かつnullを後ろにしてユーザーを取得
let desc_nulls_last: Vec = entities::user::Entity::find()
    .order_by_with_nulls(entities::user::Column::Name, Order::Desc, NullOrdering::Last)
    .all(&db)
    .await?;
  

リレーションテーブルを含めて取得する

リレーションテーブルを含めて取得するにはあらかじめentityにて関係を表すよう定義する必要があります。
// 1対1のリレーションを持つ投稿とユーザーを取得
let users_also_posts: Vec<(entities::user::Model, Option)> = entities::user::Entity::find()
    .find_also_related(entities::post::Entity)
    .all(&db)
    .await?;
// 1対多のリレーションを持つ投稿とユーザーを取得
let users_with_posts: Vec<(entities::user::Model, Vec)> = entities::user::Entity::find()
    .find_with_related(entities::post::Entity)
    .all(&db)
    .await?;
タイトルとURLをコピーしました