Skip to content

How do I declare an update_at and create_at column using sea_orm_migration #2027

Answered by fu050409
fu050409 asked this question in Q&A
Discussion options

You must be logged in to vote

Not these are my solutions now:

.col(
    ColumnDef::new(Account::CreatedAt)
        .timestamp_with_time_zone()
        .default(Expr::current_timestamp())
        .not_null(),
)
.col(
    ColumnDef::new(Account::CreatedAt)
        .timestamp_with_time_zone()
        .default(Expr::current_timestamp())
        .not_null(),
)

and updated_at will work with:

let db = manager.get_connection();

db.execute_unprepared(
    "CREATE TRIGGER updated_at
    AFTER UPDATE ON account
    FOR EACH ROW
    BEGIN
        UPDATE account
        SET updated_at = (datetime('now','localtime'))
        WHERE id = NEW.id;
    END;",
).await?;

If you have the same problem, you may should replace the plain sql …

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@fu050409
Comment options

@fu050409
Comment options

Comment options

You must be logged in to vote
1 reply
@fu050409
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@alvissraghnall
Comment options

@alvissraghnall
Comment options

Answer selected by fu050409
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants