New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify partial model queries #1597
Simplify partial model queries #1597
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution.
It looks really good!
SelectPartialModel
is exactly what I have in mind.
You are right. That's the reason why |
@billy1624 please review as well |
I have an a bit complex idea. If a model have 2 primary key struct Model{
#[sea_orm(primary_key)]
id1: i32,
#[sea_orm(primary_key)]
id2: i32,
...
} then, might the derive macro can generate the following #[derive(DerivePartialModel, FromQueryResult)]
#[sea_orm(entity = "Entity")]
struct PrimaryPartial{
id1:i32,
id2:i32
} finally, I can use a #[derive(FromQueryResult, DerivePartialModel)]
struct WithPk<E: EntityTrait, P: PartialModelTrait> {
#[sea_orm(flatten)]
pk: <<E as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::PartialModel,
#[sea_orm(flatten)]
inner: P,
} However, those code cannot using in current sea-orm, because neither |
#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Entity")]
struct TimeRecord {
_access: DateTimeLocal,
_modify: DateTimeLocal,
_delete: Option<DateTimeLocal>,
}
#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Entity")]
struct Bitmap {
_bitmap1: u64,
_bitmap2: u64,
_bitmap3: u64,
_bitmap4: u64,
}
#[derive(DerivePartialModel)]
struct NestPartialModel {
#[sea_orm(flatten)]
_record: TimeRecord,
#[sea_orm(flatten)]
_bitmap: Bitmap,
}
// manual implement
impl FromQueryResult for NestPartialModel {
fn from_query_result(
res: &sea_orm::QueryResult,
pre: &str,
) -> Result<Self, sea_orm::DbErr> {
Ok(Self {
_record: TimeRecord::from_query_result(res, pre)?,
_bitmap: Bitmap::from_query_result(res, pre)?,
})
}
} It generate SQL like following(MySQL) SELECT
`foo_table`.`access`, `foo_table`.`modify`, `foo_table`.`delete`,
`foo_table`.`bitmap1`, `foo_table`.`bitmap2`, `foo_table`.`bitmap3`, `foo_table`.`bitmap4`
FROM `foo_table` |
I think it is a good idea to support nest and generic argument for |
Just being curious, why do you have underscore in front of all attributes? |
The code snippet is in derive_test, without it will result warning. |
Yeah i.e. decoupling them would make them easier to review individually |
018a2e8
to
75fbe44
Compare
Aright, the flatten part and generic support part has been removed. I will open another PR to add |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added stream_partial_model
to Select<E>
and SelectTwo<E,F>
3b09860
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Optional Field SeaQL/sea-orm#1513 * .gitignore SeaQL/sea-orm#1334 * migration table custom name SeaQL/sea-orm#1511 * OR condition relation SeaQL/sea-orm#1433 * DerivePartialModel SeaQL/sea-orm#1597 * space for migration file naming SeaQL/sea-orm#1570 * composite key up to 12 SeaQL/sea-orm#1508 * seaography integration SeaQL/sea-orm#1599 * QuerySelect SimpleExpr SeaQL/sea-orm#1702 * sqlErr SeaQL/sea-orm#1707 * migration check SeaQL/sea-orm#1519 * postgres array SeaQL/sea-orm#1565 * param intoString SeaQL/sea-orm#1439 * **skipped** re-export SeaQL/sea-orm#1661 * ping SeaQL/sea-orm#1627 * on empty do nothing SeaQL/sea-orm#1708 * on conflict do nothing SeaQL/sea-orm#1712 * **skipped** upgrade versions * active enum fail safe SeaQL/sea-orm#1374 * relation generation check SeaQL/sea-orm#1435 * entity generation bug SeaQL/sea-schema#105 * **skipped** bug fix that does not require edits * EnumIter change SeaQL/sea-orm#1535 * completed and fixed a previous todo SeaQL/sea-orm#1570 * amended wordings and structures * Edit * Remove temp file --------- Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
🎉 Released In 0.12.1 🎉Thank you everyone for the contribution! |
PR Info
New Features
SelectColumns
: A trait can only set columnsPartialModelTrait
: A trait for partial model to select specific columnsSelect::into_partial_model
SelectTwo::into_partial_model
SelectTwoMany::into_partial_model
SelectTwoMany::stream_partial_model
need consolidate result , consolidate result need primary key. Cannot assume partial model has the pk.SelectTwoMany::all_partial_model
DerivePartialModel
automatic generate implement ofPartialModelTrait
from_col
from_expr
flatten
: can contain otherPartialModelTrait
, which will be a good solution on manual handle joinSupport generic arguments