Support Up to 6 Values Composite Primary Key (#353)

* Support up to 6 composite primary key

* Test [issues]

* Test [issues]
This commit is contained in:
Billy Chan 2021-12-04 20:57:05 +08:00 committed by GitHub
parent 41e345c3a8
commit 1229287fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 211 additions and 4 deletions

View File

@ -316,7 +316,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
path: [86, 249, 262, 319, 324]
path: [86, 249, 262, 319, 324, 352]
steps:
- uses: actions/checkout@v2

View File

@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
log = { version = "^0.4", optional = true }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.4.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.19.0", features = ["thread-safe"] }
sea-query = { version = "^0.19.0", git = "https://github.com/SeaQL/sea-query.git", branch = "sea-orm/issues/352", features = ["thread-safe"] }
sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true }

11
issues/352/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[workspace]
# A separate workspace
[package]
name = "sea-orm-issues-352"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]}

View File

@ -0,0 +1,18 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

8
issues/352/src/main.rs Normal file
View File

@ -0,0 +1,8 @@
mod unary_primary_key;
mod binary_primary_key;
mod ternary_primary_key;
mod quaternary_primary_key;
mod quinary_primary_key;
mod senary_primary_key;
pub fn main() {}

View File

@ -0,0 +1,22 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,24 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: DateTime,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,26 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: DateTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_6: DateTimeWithTimeZone,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,20 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: f64,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,16 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
pub owner: String,
pub name: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -451,6 +451,48 @@ where
}
}
impl<A, B, C, D, E> TryGetableMany for (A, B, C, D, E)
where
A: TryGetable,
B: TryGetable,
C: TryGetable,
D: TryGetable,
E: TryGetable,
{
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError> {
try_get_many_with_slice_len_of(5, cols)?;
Ok((
A::try_get(res, pre, &cols[0])?,
B::try_get(res, pre, &cols[1])?,
C::try_get(res, pre, &cols[2])?,
D::try_get(res, pre, &cols[3])?,
E::try_get(res, pre, &cols[4])?,
))
}
}
impl<A, B, C, D, E, F> TryGetableMany for (A, B, C, D, E, F)
where
A: TryGetable,
B: TryGetable,
C: TryGetable,
D: TryGetable,
E: TryGetable,
F: TryGetable,
{
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError> {
try_get_many_with_slice_len_of(6, cols)?;
Ok((
A::try_get(res, pre, &cols[0])?,
B::try_get(res, pre, &cols[1])?,
C::try_get(res, pre, &cols[2])?,
D::try_get(res, pre, &cols[3])?,
E::try_get(res, pre, &cols[4])?,
F::try_get(res, pre, &cols[5])?,
))
}
}
fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), TryGetError> {
if cols.len() < len {
Err(TryGetError::DbErr(DbErr::Query(format!(
@ -501,6 +543,8 @@ macro_rules! try_from_u64_err {
try_from_u64_err!(A, B);
try_from_u64_err!(A, B, C);
try_from_u64_err!(A, B, C, D);
try_from_u64_err!(A, B, C, D, E);
try_from_u64_err!(A, B, C, D, E, F);
macro_rules! try_from_u64_numeric {
( $type: ty ) => {
@ -540,10 +584,28 @@ macro_rules! try_from_u64_string {
try_from_u64_string!(String);
try_from_u64_err!(bool);
try_from_u64_err!(f32);
try_from_u64_err!(f64);
try_from_u64_err!(Vec<u8>);
#[cfg(feature = "with-uuid")]
try_from_u64_err!(uuid::Uuid);
#[cfg(feature = "with-json")]
try_from_u64_err!(serde_json::Value);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveDate);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveTime);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "with-rust_decimal")]
try_from_u64_err!(rust_decimal::Decimal);
#[cfg(feature = "with-uuid")]
try_from_u64_err!(uuid::Uuid);