From e24647b7a73304a5d95d6b9f5835a10eb6166fcd Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 11 Apr 2024 10:45:45 +0800 Subject: [PATCH] [SeaORM] Docs 1.0-rc (#120) * Refreshed migration schema definition (SeaQL/sea-orm#2099) * Reworked SQLite Type Mappings (SeaQL/sea-orm#2077) * `DerivePartialModel` macro attribute `entity` supports `syn::Type` as well (SeaQL/sea-orm#2137) * Added `RelationDef::from_alias()` (SeaQL/sea-orm#2146) --- .../docs/03-migration/02-writing-migration.md | 40 ++++++++++++------- .../04-generate-entity/02-entity-structure.md | 36 ++++++++--------- .../06-relation/06-custom-join-condition.md | 25 ++++++++++++ .../08-advanced-query/01-custom-select.md | 2 + 4 files changed, 70 insertions(+), 33 deletions(-) diff --git a/SeaORM/docs/03-migration/02-writing-migration.md b/SeaORM/docs/03-migration/02-writing-migration.md index cf06fa43ae2..0f3409453b9 100644 --- a/SeaORM/docs/03-migration/02-writing-migration.md +++ b/SeaORM/docs/03-migration/02-writing-migration.md @@ -118,6 +118,23 @@ Here are some common DDL snippets you may find useful. Story, } + // Remember to import `sea_orm_migration::schema::*` schema helpers into scope + use sea_orm_migration::{prelude::*, schema::*}; + + // Defining the schema with helpers + manager + .create_table( + Table::create() + .table(Post::Table) + .if_not_exists() + .col(pk_auto(Post::Id)) + .col(string(Post::Title)) + .col(string(Post::Text)) + .col(enumeration_null(Post::Category, Alias::new("category"), Category::iter())) + ) + .await + + // Or, you can define the schema without the helpers manager .create_table( Table::create() @@ -136,7 +153,6 @@ Here are some common DDL snippets you may find useful. ColumnDef::new(Post::Category) .enumeration(Alias::new("category"), Category::iter()), ) - .to_owned(), ) .await ``` @@ -167,7 +183,6 @@ Here are some common DDL snippets you may find useful. Type::create() .as_enum(CategoryEnum) .values(CategoryVariants::iter()) - .to_owned(), ) .await?; ``` @@ -275,6 +290,9 @@ impl MigrationTrait for Migration { You can combine multiple changes within both up and down migration functions. Here is a complete example: ```rust +// Remember to import `sea_orm_migration::schema::*` schema helpers into scope +use sea_orm_migration::{prelude::*, schema::*}; + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager @@ -282,16 +300,9 @@ async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { sea_query::Table::create() .table(Post::Table) .if_not_exists() - .col( - ColumnDef::new(Post::Id) - .integer() - .not_null() - .auto_increment() - .primary_key() - ) - .col(ColumnDef::new(Post::Title).string().not_null()) - .col(ColumnDef::new(Post::Text).string().not_null()) - .to_owned() + .col(pk_auto(Post::Id)) + .col(string(Post::Title)) + .col(string(Post::Text)) ) .await?; @@ -302,7 +313,6 @@ async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { .name("idx-post_title") .table(Post::Table) .col(Post::Title) - .to_owned(), ) .await?; @@ -315,10 +325,10 @@ and here we have the matching down function: ```rust async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager.drop_index(Index::drop().name("idx-post-title").to_owned()) + manager.drop_index(Index::drop().name("idx-post-title")) .await?; - manager.drop_table(Table::drop().table(Post::Table).to_owned()) + manager.drop_table(Table::drop().table(Post::Table)) .await?; Ok(()) // All good! diff --git a/SeaORM/docs/04-generate-entity/02-entity-structure.md b/SeaORM/docs/04-generate-entity/02-entity-structure.md index d729ff7604e..8a10ee0a726 100644 --- a/SeaORM/docs/04-generate-entity/02-entity-structure.md +++ b/SeaORM/docs/04-generate-entity/02-entity-structure.md @@ -72,32 +72,32 @@ For the mappings of Rust primitive data types. | Rust type | Database Type
([`ColumnType`](https://docs.rs/sea-orm/*/sea_orm/entity/enum.ColumnType.html)) | SQLite
datatype | MySQL
datatype | PostgreSQL
datatype | | --------- | --------- | --------- | --------- | --------- | -| `String` | Char | text | char | char | -| `String` | String | text | varchar | varchar | -| `i8` | TinyInteger | integer | tinyint | char | -| `u8` | TinyUnsigned | integer | tinyint unsigned | N/A | -| `i16` | SmallInteger | integer | smallint | smallint | -| `u16` | SmallUnsigned | integer | smallint unsigned | N/A | +| `String` | Char | char | char | char | +| `String` | String | varchar | varchar | varchar | +| `i8` | TinyInteger | tinyint | tinyint | char | +| `u8` | TinyUnsigned | tinyint | tinyint unsigned | N/A | +| `i16` | SmallInteger | smallint | smallint | smallint | +| `u16` | SmallUnsigned | smallint | smallint unsigned | N/A | | `i32` | Integer | integer | int | integer | | `u32` | Unsigned | integer | int unsigned | N/A | -| `i64` | BigInteger | integer | bigint | bigint | -| `u64` | BigUnsigned | N/A | bigint unsigned | N/A | -| `f32` | Float | real | float | real | -| `f64` | Double | real | double | double precision | -| `bool` | Boolean | integer | bool | bool | +| `i64` | BigInteger | bigint | bigint | bigint | +| `u64` | BigUnsigned | bigint | bigint unsigned | N/A | +| `f32` | Float | float | float | real | +| `f64` | Double | double | double | double precision | +| `bool` | Boolean | boolean | bool | bool | | `Vec` | Binary | blob | blob | bytea | For the mappings of Rust non-primitive data types. You can check [`entity/prelude.rs`](https://github.com/SeaQL/sea-orm/blob/master/src/entity/prelude.rs) for all of the reexported types. | Rust type | Database Type
([`ColumnType`](https://docs.rs/sea-orm/*/sea_orm/entity/enum.ColumnType.html)) | SQLite
datatype | MySQL
datatype | PostgreSQL
datatype | | --------- | --------- | --------- | --------- | --------- | -| `Date`: chrono::NaiveDate
`TimeDate`: time::Date | Date | text | date | date | -| `Time`: chrono::NaiveTime
`TimeTime`: time::Time | Time | text | time | time | -| `DateTime`: chrono::NaiveDateTime
`TimeDateTime`: time::PrimitiveDateTime | DateTime | text | datetime | timestamp | -| `DateTimeLocal`: chrono::DateTime<Local>
`DateTimeUtc`: chrono::DateTime<Utc> | Timestamp | text | timestamp | N/A | -| `DateTimeWithTimeZone`: chrono::DateTime<FixedOffset>
`TimeDateTimeWithTimeZone`: time::OffsetDateTime | TimestampWithTimeZone | text | timestamp | timestamp with time zone | -| `Uuid`: uuid::Uuid, uuid::fmt::Braced, uuid::fmt::Hyphenated, uuid::fmt::Simple, uuid::fmt::Urn | Uuid | text | binary(16) | uuid | -| `Json`: serde_json::Value | Json | text | json | json | +| `Date`: chrono::NaiveDate
`TimeDate`: time::Date | Date | date_text | date | date | +| `Time`: chrono::NaiveTime
`TimeTime`: time::Time | Time | time_text | time | time | +| `DateTime`: chrono::NaiveDateTime
`TimeDateTime`: time::PrimitiveDateTime | DateTime | datetime_text | datetime | timestamp | +| `DateTimeLocal`: chrono::DateTime<Local>
`DateTimeUtc`: chrono::DateTime<Utc> | Timestamp | timestamp_text | timestamp | N/A | +| `DateTimeWithTimeZone`: chrono::DateTime<FixedOffset>
`TimeDateTimeWithTimeZone`: time::OffsetDateTime | TimestampWithTimeZone | timestamp_with_timezone_text | timestamp | timestamp with time zone | +| `Uuid`: uuid::Uuid, uuid::fmt::Braced, uuid::fmt::Hyphenated, uuid::fmt::Simple, uuid::fmt::Urn | Uuid | uuid_text | binary(16) | uuid | +| `Json`: serde_json::Value | Json | json_text | json | json | | `Decimal`: rust_decimal::Decimal | Decimal | real | decimal | decimal | You can override the default mappings between a Rust type and `ColumnType` by the `column_type` attribute. diff --git a/SeaORM/docs/06-relation/06-custom-join-condition.md b/SeaORM/docs/06-relation/06-custom-join-condition.md index 3d623de87c8..8844164f391 100644 --- a/SeaORM/docs/06-relation/06-custom-join-condition.md +++ b/SeaORM/docs/06-relation/06-custom-join-condition.md @@ -206,4 +206,29 @@ assert_eq!( ] .join(" ") ); +``` + +Specify table alias in the join statement: + +```rust +assert_eq!( + cake::Entity::find() + .join_as( + JoinType::LeftJoin, + cake_filling::Relation::Cake.def().rev(), + cf.clone() + ) + .join( + JoinType::LeftJoin, + cake_filling::Relation::Filling.def().from_alias(cf) + ) + .build(DbBackend::MySql) + .to_string(), + [ + "SELECT `cake`.`id`, `cake`.`name` FROM `cake`", + "LEFT JOIN `cake_filling` AS `cf` ON `cake`.`id` = `cf`.`cake_id`", + "LEFT JOIN `filling` ON `cf`.`filling_id` = `filling`.`id`", + ] + .join(" ") +); ``` \ No newline at end of file diff --git a/SeaORM/docs/08-advanced-query/01-custom-select.md b/SeaORM/docs/08-advanced-query/01-custom-select.md index 2dd87bcff66..a930bc419b4 100644 --- a/SeaORM/docs/08-advanced-query/01-custom-select.md +++ b/SeaORM/docs/08-advanced-query/01-custom-select.md @@ -194,6 +194,8 @@ You can define a partial model, and the corresponding columns will be automatica ```rust #[derive(DerivePartialModel, FromQueryResult)] #[sea_orm(entity = "User")] +// `DerivePartialModel` macro attribute `entity` supports `syn::Type` as well +#[sea_orm(entity = "::Entity")] struct PartialUser { pub id: i32, pub avatar: String,