This content originally appeared on DEV Community and was authored by Augusts Bautra
Today I used the opportunity to try out Rails' #add_check_constraint migration helper to constrain a new coefficient column's permitted values to only positive ones.
It's really simple!
add_check_constraint :my_things, "some_coefficient > 0",
name: "my_things_some_coefficient_positive",
if_not_exists: true
This technique will help improve the data consistency and quality in the Rails app I maintain, alongside such long-standing techniques as disallowing NULLs, limiting length, and adding foreign-key constraints.
This content originally appeared on DEV Community and was authored by Augusts Bautra
Augusts Bautra | Sciencx (2025-10-23T08:06:02+00:00) TIL: DB constraints for column values in Rails. Retrieved from https://www.scien.cx/2025/10/23/til-db-constraints-for-column-values-in-rails/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.