Skip to content
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

Fixup #6

Open
wants to merge 1 commit into
base: auto-update-timestamp-column
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/schema/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use sea_query::{
extension::postgres::{Type, TypeCreateStatement},
ColumnDef, Iden, Index, IndexCreateStatement, SeaRc, TableCreateStatement,
ColumnDef, Expr, Iden, Index, IndexCreateStatement, SeaRc, TableCreateStatement,
};

impl Schema {
Expand Down Expand Up @@ -210,9 +210,11 @@ where
}
if let Some(value) = orm_column_def.default_value {
column_def.default(value);
} else if orm_column_def.created_at || orm_column_def.updated_at {
column_def.default(Expr::current_timestamp());
}
if let Some(value) = orm_column_def.extra {
column_def.default(value);
column_def.extra(value);
}
for primary_key in E::PrimaryKey::iter() {
if column.to_string() == primary_key.into_column().to_string() {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/features/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub struct Model {
pub id: i32,
pub pay: String,
pub amount: f64,
#[sea_orm(updated_at, nullable, extra = "DEFAULT CURRENT_TIMESTAMP")]
#[sea_orm(updated_at)]
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(created_at, nullable, extra = "DEFAULT CURRENT_TIMESTAMP")]
#[sea_orm(created_at)]
pub created_at: DateTimeWithTimeZone,
}

Expand Down
8 changes: 5 additions & 3 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use sea_orm::{
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
ExecResult, Schema,
};
use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement, IntoIden};
use sea_query::{
extension::postgres::Type, Alias, ColumnDef, Expr, ForeignKeyCreateStatement, IntoIden,
};

pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
let db_backend = db.get_database_backend();
Expand Down Expand Up @@ -515,12 +517,12 @@ pub async fn create_check_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(
ColumnDef::new(check::Column::UpdatedAt)
.timestamp_with_time_zone()
.default("CURRENT_TIMESTAMP"),
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(check::Column::CreatedAt)
.timestamp_with_time_zone()
.default("CURRENT_TIMESTAMP"),
.default(Expr::current_timestamp()),
)
.to_owned();

Expand Down