-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
auto set timestamp column when update & insert #854
base: master
Are you sure you want to change the base?
auto set timestamp column when update & insert #854
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.
Hey @liberwang1013, sorry for the delay. Thanks for the contributions!!
I have added some test cases, refactored a few places as well. Please review and then cherrypick :)
Hi @billy1624 , thank you for your commit. I have already cherrypicked your commit, your commit is great. But, now we have to face a new problem that we didn't pass all checks. I have searched some documents try to figure out what's the problem . In this case, it seems that default value the system given is conflicted with msyql default
I think there are three ways to fix it
what's your opinion? |
Yes, actually if you want a robust timestamps, the best way is to rely on DB's native |
2869439
to
d9d77dc
Compare
Note that not all database support such |
Okay, we have two problems here
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "check")]
pub struct Model {
...
#[sea_orm(updated_at, default_expr = "Func::current_timestamp()")]
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(created_at, default_expr = "Func::current_timestamp()")]
pub created_at: DateTimeWithTimeZone,
} Also, we could provide a shorthand for it #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "check")]
pub struct Model {
...
#[sea_orm(updated_at, default_current_timestamp)]
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(created_at, default_current_timestamp)]
pub created_at: DateTimeWithTimeZone,
} Related issues:
For example,
Related issues: |
In this way, I prefer using |
I think I have found a better way to do this: support |
7df929b
to
37de813
Compare
I'd say the |
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.
Thanks!! @liberwang1013
https://docs.rs/sea-query/latest/sea_query/table/struct.ColumnDef.html#method.default in SeaQuery now also accpets an let table = Table::create()
.table(Char::Table)
.col(ColumnDef::new(Char::FontId).integer().default(12i32))
.col(
ColumnDef::new(Char::CreatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
[
"CREATE TABLE `character` (",
"`font_id` int DEFAULT 12,",
"`created_at` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL",
")",
]
.join(" ")
); |
…at in update statement
37de813
to
fa4b65d
Compare
src/schema/entity.rs
Outdated
if let Some(value) = orm_column_def.extra { | ||
column_def.default(value); | ||
} |
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.
Found a typo
if let Some(value) = orm_column_def.extra { | |
column_def.default(value); | |
} | |
if let Some(value) = orm_column_def.extra { | |
column_def.extra(value); | |
} |
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.
Hi @billy1624 thanks for your comment,
what about replaced with column_def.default(SimpleExpr::CustomWithExpr(value, vec![]));
, I found the generated default expr in schema is going to quoted with '
in this(column_def.default(value);
) way.
Hey @liberwang1013, please check liberwang1013#6 |
hi @tyt2y3 , the code has been updated, please review it. |
Sorry but this will not be released on 0.11 |
Any updates on this functionality getting in, or has something else similar made its way into the crate? |
Any update? 🤯 |
PR Info
created_at
&updated_at
timestamp columns on update #827Adds
created_at
&updated_at
attributes support when define modelMore Details
In this implement, the timestamp column type should be defined as
TIMESTAMP
orTIMESTAMPTZ
, so the Model must be defined line below