Add custom field name tag to models#486
Add custom field name tag to models#486xelab04 wants to merge 4 commits intocot-rs:raw-field-namesfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
m4tx
left a comment
There was a problem hiding this comment.
Ha, looks easy! Please make sure to add tests for this, but otherwise it looks good.
If you feel like it, it might be useful to add similar functionality to specify a custom table names for models, too.
Nah, it's completely fine - after all, it builds on top of my PR. I'll try to merge that one in the next day or two, so that we'll be able to merge yours, too. The part you're changing, however, might change in my PR - I've just realized there's |
uses field_name rather than table_name Co-authored-by: Mateusz Maćkowski <mateusz@mackowski.org>
|
I have added a test, I think it should be alright? |
I noticed that feature is already there as |
You're actually right, it's already implemented (but propably not well documented)! For models defined as: #[model]
pub struct Link {
// <SNIPPED>
}
#[model(table_name = "custom-name")]
pub struct Link2 {
// <SNIPPED>
}It created the following migration: //! Generated by cot CLI 0.5.0 on 2026-02-25 10:57:51+00:00
#[derive(Debug, Copy, Clone)]
pub(super) struct Migration;
impl ::cot::db::migrations::Migration for Migration {
const APP_NAME: &'static str = "my_project";
const MIGRATION_NAME: &'static str = "m_0001_initial";
const DEPENDENCIES: &'static [::cot::db::migrations::MigrationDependency] = &[];
const OPERATIONS: &'static [::cot::db::migrations::Operation] = &[
::cot::db::migrations::Operation::create_model()
.table_name(::cot::db::Identifier::new("my_project__link"))
.fields(
// <SNIPPED>
)
.build(),
::cot::db::migrations::Operation::create_model()
.table_name(::cot::db::Identifier::new("my_project__custom-name"))
.fields(
// <SNIPPED>
)
.build(),
];
}So you can focus on adding that customizability only to field names! |
I admit maybe opening this PR to the raw field names might not be appropriate.
I have added a
table_nameflag, which would allow setting a custom name for the db field used for a parameter.Usage should be something like:
So instead of the db field being "hello", it will be "world" instead.
I did run
cargo test --all-featuresthough I'll have to write some tests for this feature too.