Skip to content

Add custom field name tag to models#486

Open
xelab04 wants to merge 4 commits intocot-rs:raw-field-namesfrom
xelab04:raw-field-names
Open

Add custom field name tag to models#486
xelab04 wants to merge 4 commits intocot-rs:raw-field-namesfrom
xelab04:raw-field-names

Conversation

@xelab04
Copy link

@xelab04 xelab04 commented Feb 22, 2026

I admit maybe opening this PR to the raw field names might not be appropriate.
I have added a table_name flag, which would allow setting a custom name for the db field used for a parameter.

Usage should be something like:

#[model]
pub struct Meetup {
    #[model(primary_key)]
    pub id: Auto<i32>,
    #[model(table_name = "world")]
    pub hello: LimitedString<255>,

So instead of the db field being "hello", it will be "world" instead.
I did run cargo test --all-features though I'll have to write some tests for this feature too.

@github-actions github-actions bot added the C-codegen Crate: cot-codegen label Feb 22, 2026
@codecov
Copy link

codecov bot commented Feb 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
rust 89.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
cot-codegen/src/model.rs 94.26% <100.00%> (+0.06%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@m4tx m4tx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@m4tx
Copy link
Member

m4tx commented Feb 23, 2026

I admit maybe opening this PR to the raw field names might not be appropriate.

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 Ident::unraw which will simplify the code a little bit.

xelab04 and others added 2 commits February 24, 2026 11:01
uses field_name rather than table_name

Co-authored-by: Mateusz Maćkowski <mateusz@mackowski.org>
@xelab04
Copy link
Author

xelab04 commented Feb 24, 2026

I have added a test, I think it should be alright?
I will alter my PR to fit the unraw if necessary.

@xelab04
Copy link
Author

xelab04 commented Feb 24, 2026

add similar functionality to specify a custom table names for models

I noticed that feature is already there as #[model(table_name = "helloworld")] and is around line 97 of the model.rs codegen file.

@seqre
Copy link
Member

seqre commented Feb 25, 2026

add similar functionality to specify a custom table names for models

I noticed that feature is already there as #[model(table_name = "helloworld")] and is around line 97 of the model.rs codegen file.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-codegen Crate: cot-codegen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants