Find by composite key
This commit is contained in:
parent
92ac1098cb
commit
13bad8eb17
@ -1,8 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ColumnTrait, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryHelper, RelationBuilder,
|
ColumnTrait, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryHelper, RelationBuilder,
|
||||||
RelationTrait, RelationType, Select,
|
RelationTrait, RelationType, Select
|
||||||
};
|
};
|
||||||
use sea_query::{Iden, Value};
|
use sea_query::{Iden, IntoValueTuple};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
pub use strum::IntoEnumIterator as Iterable;
|
pub use strum::IntoEnumIterator as Iterable;
|
||||||
|
|
||||||
@ -21,10 +21,6 @@ pub trait EntityTrait: EntityName {
|
|||||||
|
|
||||||
type PrimaryKey: PrimaryKeyTrait + Iterable;
|
type PrimaryKey: PrimaryKeyTrait + Iterable;
|
||||||
|
|
||||||
fn auto_increment() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
|
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
|
||||||
where
|
where
|
||||||
R: EntityTrait,
|
R: EntityTrait,
|
||||||
@ -64,18 +60,37 @@ pub trait EntityTrait: EntityName {
|
|||||||
/// r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = 11"#
|
/// r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = 11"#
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
fn find_by<V>(v: V) -> Select<Self>
|
/// Find by composite key
|
||||||
|
/// ```
|
||||||
|
/// use sea_orm::{ColumnTrait, EntityTrait, tests_cfg::cake_filling, sea_query::PostgresQueryBuilder};
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// cake_filling::Entity::find_by((2, 3))
|
||||||
|
/// .build(PostgresQueryBuilder)
|
||||||
|
/// .to_string(),
|
||||||
|
/// [
|
||||||
|
/// r#"SELECT "cake_filling"."cake_id", "cake_filling"."filling_id" FROM "cake_filling""#,
|
||||||
|
/// r#"WHERE "cake_filling"."cake_id" = 2 AND "cake_filling"."filling_id" = 3"#,
|
||||||
|
/// ].join(" ")
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
fn find_by<V>(values: V) -> Select<Self>
|
||||||
where
|
where
|
||||||
V: Into<Value>,
|
V: IntoValueTuple,
|
||||||
Self::PrimaryKey: PrimaryKeyOfModel<Self::Model>,
|
Self::PrimaryKey: PrimaryKeyOfModel<Self::Model>,
|
||||||
{
|
{
|
||||||
let mut select = Self::find();
|
let mut select = Self::find();
|
||||||
if let Some(key) = Self::PrimaryKey::iter().next() {
|
let mut keys = Self::PrimaryKey::iter();
|
||||||
// TODO: supporting composite primary key
|
for v in values.into_value_tuple() {
|
||||||
|
if let Some(key) = keys.next() {
|
||||||
let col = key.into_column();
|
let col = key.into_column();
|
||||||
select = select.filter(col.eq(v));
|
select = select.filter(col.eq(v));
|
||||||
} else {
|
} else {
|
||||||
panic!("undefined primary key");
|
panic!("primary key arity mismatch");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if keys.next().is_some() {
|
||||||
|
panic!("primary key arity mismatch");
|
||||||
}
|
}
|
||||||
select
|
select
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user