-
I uses the latest Sea-ORM on crates.io, and uses Sqlite3 as my database backend. Hello! I tried to look for information about my problem in the issues and discussions of sea-orm, but may due to the limitations of my English, I really didn't find enough information about it. Please forgive me if there is already a duplicate question. I saw an issue where it was mentioned that I could implement I want to build a column that stores the creation time and update time, here is my code: // In up function
manager.create_table(
Table::create()
.table(Account::Table)
.if_not_exists()
.col(
ColumnDef::new(Account::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Account::Username)
.string()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(Account::Password).string().not_null())
.col(ColumnDef::new(Account::Nickname).string().null())
.col(
ColumnDef::new(Account::CreateTime)
.timestamp()
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
)
.col(
ColumnDef::new(Account::UpdateTime)
.timestamp()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()) // Now it's the same as the CreateTime field, but I need it to update automatically when the data gets updated
.not_null()
)
.to_owned(),
)
.await And this is the model code: #[derive(DeriveIden)]
enum Account {
Table,
Id,
Username,
Password,
Nickname,
CreateTime,
UpdateTime,
} I deeply suspect that this is due to me missing my database class. :( Thank you very much ! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
After doing more searching, I realized that the |
Beta Was this translation helpful? Give feedback.
-
I found a another solution to implement .col(
ColumnDef::new(Account::CreateTime)
.timestamp_with_time_zone()
.default(Expr::current_timestamp())
.not_null(),
) |
Beta Was this translation helpful? Give feedback.
-
https://github.com/SeaQL/sea-orm/pull/854/files#diff-50b187da806a981ede479097b8adc4cc78b5dfbe6d7d42495e914b7133b632e9 These are the links that mentioned my issue, but I found it seems that Sea-ORM haven't supported May be I should update the |
Beta Was this translation helpful? Give feedback.
-
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 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 command with your database's. |
Beta Was this translation helpful? Give feedback.
Not these are my solutions now:
and
updated_at
will work with:If you have the same problem, you may should replace the plain sql …