Skip to content

How to use builtin function of database in sea-orm? #2295

Answered by Expurple
ofzo asked this question in Q&A
Discussion options

You must be logged in to vote

Builtin functions can be expressed using sea_query::Expr::cust("UUID()").

But if you have a SeaORM entity and want to use it normally through an ActiveModel, I only know how to sneak an expression into an UPDATE:

users::Entity::update_many()
    .col_expr(users::Column::Uid, Expr::cust("UUID()"))
    .filter(...)
    .exec(&conn)
    .await?;

Insert APIs seem to only accept whole ActiveModels, where fields are ActiveValues, which can only store Rust values and not arbitrary SQL expressions.


If your use case only involves the UUID() function, there are a few obvious workarounds:

  • Set a DEFAULT in the DB and keep this column NotSet when you insert the ActiveModel.
  • Generate and set the UUID…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ofzo
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