Find models belongs to
This commit is contained in:
parent
58f4059c3e
commit
8ab34882bd
@ -1,5 +1,5 @@
|
|||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic,
|
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
|
||||||
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
|
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -98,6 +98,15 @@ impl IdenStatic for PrimaryKey {
|
|||||||
// TODO: implement with derive macro
|
// TODO: implement with derive macro
|
||||||
impl PrimaryKeyTrait for PrimaryKey {}
|
impl PrimaryKeyTrait for PrimaryKey {}
|
||||||
|
|
||||||
|
// TODO: implement with derive macro
|
||||||
|
impl PrimaryKeyOfModel<Model> for PrimaryKey {
|
||||||
|
fn into_column(self) -> <Model as ModelTrait>::Column {
|
||||||
|
match self {
|
||||||
|
Self::Id => Column::Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
match self {
|
match self {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic,
|
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
|
||||||
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
|
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,6 +103,15 @@ impl IdenStatic for PrimaryKey {
|
|||||||
// TODO: implement with derive macro
|
// TODO: implement with derive macro
|
||||||
impl PrimaryKeyTrait for PrimaryKey {}
|
impl PrimaryKeyTrait for PrimaryKey {}
|
||||||
|
|
||||||
|
// TODO: implement with derive macro
|
||||||
|
impl PrimaryKeyOfModel<Model> for PrimaryKey {
|
||||||
|
fn into_column(self) -> <Model as ModelTrait>::Column {
|
||||||
|
match self {
|
||||||
|
Self::Id => Column::Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
panic!()
|
panic!()
|
||||||
|
@ -32,4 +32,14 @@ async fn main() {
|
|||||||
println!();
|
println!();
|
||||||
println!("{:?}", cheese);
|
println!("{:?}", cheese);
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
|
println!("find models belongs to");
|
||||||
|
|
||||||
|
let fruits = cheese.find_fruit().all(&db).await.unwrap();
|
||||||
|
|
||||||
|
println!();
|
||||||
|
for ff in fruits.iter() {
|
||||||
|
println!("{:?}", ff);
|
||||||
|
println!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
use super::IdenStatic;
|
use super::{ModelTrait, IdenStatic};
|
||||||
|
|
||||||
pub trait PrimaryKeyTrait: IdenStatic {}
|
pub trait PrimaryKeyTrait: IdenStatic {}
|
||||||
|
|
||||||
|
pub trait PrimaryKeyOfModel<M>
|
||||||
|
where
|
||||||
|
M: ModelTrait,
|
||||||
|
{
|
||||||
|
fn into_column(self) -> M::Column;
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{EntityTrait, Identity, Iterable, RelationDef, RelationTrait, Statement, Related};
|
use crate::{EntityTrait, Identity, Iterable, PrimaryKeyOfModel, RelationDef, RelationTrait, Statement, Related};
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
pub use sea_query::JoinType;
|
pub use sea_query::JoinType;
|
||||||
@ -88,13 +88,16 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn belongs_to<R>(self, model: &R::Model) -> Self
|
pub fn belongs_to<R>(self, model: &R::Model) -> Self
|
||||||
where R: EntityTrait + Related<E> {
|
where R: EntityTrait + Related<E>, R::PrimaryKey: PrimaryKeyOfModel<R::Model> {
|
||||||
// match R::primary_key() {
|
use crate::{ColumnTrait, ModelTrait};
|
||||||
// Identity::Unary(iden) => {
|
|
||||||
// model.get(iden)
|
if let Some(key) = R::PrimaryKey::iter().next() {
|
||||||
// }
|
// TODO: supporting composite primary key
|
||||||
// };
|
let col = key.into_column();
|
||||||
self
|
self.filter(col.eq(model.get(col)))
|
||||||
|
} else {
|
||||||
|
panic!("undefined primary key");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ```
|
/// ```
|
||||||
@ -243,4 +246,25 @@ mod tests {
|
|||||||
.join(" ")
|
.join(" ")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn join_5() {
|
||||||
|
|
||||||
|
let cake_model = cake::Model {
|
||||||
|
id: 12,
|
||||||
|
name: "".to_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cake_model.find_fruit()
|
||||||
|
.build(MysqlQueryBuilder)
|
||||||
|
.to_string(),
|
||||||
|
[
|
||||||
|
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
|
||||||
|
"INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`",
|
||||||
|
"WHERE `cake`.`id` = 12",
|
||||||
|
]
|
||||||
|
.join(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic,
|
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
|
||||||
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
|
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -98,6 +98,15 @@ impl IdenStatic for PrimaryKey {
|
|||||||
// TODO: implement with derive macro
|
// TODO: implement with derive macro
|
||||||
impl PrimaryKeyTrait for PrimaryKey {}
|
impl PrimaryKeyTrait for PrimaryKey {}
|
||||||
|
|
||||||
|
// TODO: implement with derive macro
|
||||||
|
impl PrimaryKeyOfModel<Model> for PrimaryKey {
|
||||||
|
fn into_column(self) -> <Model as ModelTrait>::Column {
|
||||||
|
match self {
|
||||||
|
Self::Id => Column::Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
match self {
|
match self {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic,
|
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
|
||||||
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
|
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,6 +103,15 @@ impl IdenStatic for PrimaryKey {
|
|||||||
// TODO: implement with derive macro
|
// TODO: implement with derive macro
|
||||||
impl PrimaryKeyTrait for PrimaryKey {}
|
impl PrimaryKeyTrait for PrimaryKey {}
|
||||||
|
|
||||||
|
// TODO: implement with derive macro
|
||||||
|
impl PrimaryKeyOfModel<Model> for PrimaryKey {
|
||||||
|
fn into_column(self) -> <Model as ModelTrait>::Column {
|
||||||
|
match self {
|
||||||
|
Self::Id => Column::Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
panic!()
|
panic!()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user