PrimaryKey auto_increment

This commit is contained in:
Chris Tsang 2021-06-12 19:03:29 +08:00
parent 991b04580c
commit 71d0cde771
11 changed files with 56 additions and 5 deletions

View File

@ -26,6 +26,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Fruit,

View File

@ -27,6 +27,12 @@ pub enum PrimaryKey {
FillingId,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
false
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Cake,

View File

@ -26,6 +26,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

View File

@ -28,6 +28,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

View File

@ -45,8 +45,6 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
}
}
impl sea_orm::PrimaryKeyTrait for #ident {}
impl sea_orm::PrimaryKeyToColumn for #ident {
type Column = Column;

View File

@ -1,4 +1,7 @@
use crate::{Database, DeleteResult, EntityTrait, ExecErr, Iterable, PrimaryKeyToColumn, Value};
use crate::{
Database, DeleteResult, EntityTrait, ExecErr, Iterable, PrimaryKeyToColumn, PrimaryKeyTrait,
Value,
};
use std::fmt::Debug;
#[derive(Clone, Debug, Default)]
@ -258,7 +261,7 @@ where
let exec = E::insert(am).exec(db);
let res = exec.await?;
// TODO: if the entity does not have auto increment primary key, then last_insert_id is a wrong value
if res.last_insert_id != 0 {
if <E::PrimaryKey as PrimaryKeyTrait>::auto_increment() && res.last_insert_id != 0 {
let find = E::find_by(res.last_insert_id).one(db);
let res = find.await;
let model: Option<E::Model> = res.map_err(|_| ExecErr)?;

View File

@ -1,6 +1,8 @@
use super::{ColumnTrait, IdenStatic, Iterable};
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
pub trait PrimaryKeyTrait: IdenStatic + Iterable {
fn auto_increment() -> bool;
}
pub trait PrimaryKeyToColumn {
type Column: ColumnTrait;

View File

@ -27,6 +27,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Fruit,

View File

@ -28,6 +28,12 @@ pub enum PrimaryKey {
FillingId,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
false
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Cake,

View File

@ -27,6 +27,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

View File

@ -29,6 +29,12 @@ pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}