Derive Macro TableName

Source
#[derive(TableName)]
{
    // Attributes available to this derive:
    #[table]
}
Expand description

The TableName derive macro automatically generates a table_name function for a struct, returning the value specified in the table attribute.

§Syntax

use sqlx_template::TableName;

#[derive(TableName)]
#[table("users")]
pub struct User {
    pub id: i32,
    pub name: String,
}

§Attributes

  • table: Specifies the name of the table as a string (e.g., #[table("users")]).

§Function Signature

The macro generates a const function named table_name() which returns a &'static str containing the table name.

§Example Usage

use sqlx_template::TableName;

#[derive(TableName)]
#[table("users")]
pub struct User {
    pub id: i32,
    pub name: String,
    pub age: i32,
}

fn main() {
    assert_eq!(User::table_name(), "users");
}

§Note

This macro is often used in combination with other sqlx-template macros to provide a consistent way to reference table names throughout your application.