Support Vec<u8>
primary key
This commit is contained in:
parent
7309f8a624
commit
892c0fe57d
@ -539,5 +539,7 @@ macro_rules! try_from_u64_string {
|
|||||||
|
|
||||||
try_from_u64_string!(String);
|
try_from_u64_string!(String);
|
||||||
|
|
||||||
|
try_from_u64_err!(Vec<u8>);
|
||||||
|
|
||||||
#[cfg(feature = "with-uuid")]
|
#[cfg(feature = "with-uuid")]
|
||||||
try_from_u64_err!(uuid::Uuid);
|
try_from_u64_err!(uuid::Uuid);
|
||||||
|
74
tests/byte_primary_key_tests.rs
Normal file
74
tests/byte_primary_key_tests.rs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
pub mod common;
|
||||||
|
|
||||||
|
pub use common::{features::*, setup::*, TestContext};
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
||||||
|
|
||||||
|
#[sea_orm_macros::test]
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "sqlx-mysql",
|
||||||
|
feature = "sqlx-sqlite",
|
||||||
|
feature = "sqlx-postgres"
|
||||||
|
))]
|
||||||
|
async fn main() -> Result<(), DbErr> {
|
||||||
|
let ctx = TestContext::new("byte_primary_key_tests").await;
|
||||||
|
create_tables(&ctx.db).await?;
|
||||||
|
create_and_update(&ctx.db).await?;
|
||||||
|
ctx.delete().await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
|
use common::features::byte_primary_key::*;
|
||||||
|
|
||||||
|
let model = Model {
|
||||||
|
id: vec![1],
|
||||||
|
value: "First Row".to_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = Entity::insert(model.clone().into_active_model())
|
||||||
|
.exec(db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert_eq!(Entity::find().one(db).await?, Some(model.clone()));
|
||||||
|
|
||||||
|
assert_eq!(res.last_insert_id, model.id);
|
||||||
|
|
||||||
|
let updated_active_model = ActiveModel {
|
||||||
|
value: Set("First Row (Updated)".to_owned()),
|
||||||
|
..model.clone().into_active_model()
|
||||||
|
};
|
||||||
|
|
||||||
|
let update_res = Entity::update(updated_active_model.clone())
|
||||||
|
.filter(Column::Id.eq(vec![1, 4]))
|
||||||
|
.exec(db)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
update_res,
|
||||||
|
Err(DbErr::RecordNotFound(
|
||||||
|
"None of the database rows are affected".to_owned()
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
|
let update_res = Entity::update(updated_active_model.clone())
|
||||||
|
.filter(Column::Id.eq(vec![1]))
|
||||||
|
.exec(db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert_eq!(update_res, updated_active_model);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Entity::find()
|
||||||
|
.filter(Column::Id.eq(vec![1]))
|
||||||
|
.one(db)
|
||||||
|
.await?,
|
||||||
|
Some(Model {
|
||||||
|
id: vec![1],
|
||||||
|
value: "First Row (Updated)".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
14
tests/common/features/byte_primary_key.rs
Normal file
14
tests/common/features/byte_primary_key.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||||
|
#[sea_orm(table_name = "byte_primary_key")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub id: Vec<u8>,
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
@ -1,10 +1,12 @@
|
|||||||
pub mod applog;
|
pub mod applog;
|
||||||
|
pub mod byte_primary_key;
|
||||||
pub mod metadata;
|
pub mod metadata;
|
||||||
pub mod repository;
|
pub mod repository;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod self_join;
|
pub mod self_join;
|
||||||
|
|
||||||
pub use applog::Entity as Applog;
|
pub use applog::Entity as Applog;
|
||||||
|
pub use byte_primary_key::Entity as BytePrimaryKey;
|
||||||
pub use metadata::Entity as Metadata;
|
pub use metadata::Entity as Metadata;
|
||||||
pub use repository::Entity as Repository;
|
pub use repository::Entity as Repository;
|
||||||
pub use schema::*;
|
pub use schema::*;
|
||||||
|
@ -10,6 +10,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
create_metadata_table(db).await?;
|
create_metadata_table(db).await?;
|
||||||
create_repository_table(db).await?;
|
create_repository_table(db).await?;
|
||||||
create_self_join_table(db).await?;
|
create_self_join_table(db).await?;
|
||||||
|
create_byte_primary_key_table(db).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -100,3 +101,22 @@ pub async fn create_self_join_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|||||||
|
|
||||||
create_table(db, &stmt, SelfJoin).await
|
create_table(db, &stmt, SelfJoin).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_byte_primary_key_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let stmt = sea_query::Table::create()
|
||||||
|
.table(byte_primary_key::Entity)
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(byte_primary_key::Column::Id)
|
||||||
|
.binary()
|
||||||
|
.not_null()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(byte_primary_key::Column::Value)
|
||||||
|
.string()
|
||||||
|
.not_null(),
|
||||||
|
)
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
create_table(db, &stmt, BytePrimaryKey).await
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user