-
Notifications
You must be signed in to change notification settings - Fork 58
Description
Currently, the model mapping functionality relies on the model's field names directly corresponding to the database column names. The existing field attributes (#[key], #[auto], #[unique], #[index]) are very useful for defining key constraints and indexing, but they do not address the scenario where the desired model field name differs from the legacy or externally mandated database column name.
This limitation forces developers to name their model fields exactly as they appear in the database, which can lead to violations of application-level naming conventions (e.g., using snake_case in the database vs. camelCase in the model) and reduce the clarity and expressiveness of the model code.
I propose the introduction of a new field attribute that allows developers to explicitly specify the corresponding database column name for a model field. A good candidate for this attribute would be something like #[column("...")] or #[map("...").
This would allow the mapping logic to look for this attribute first and, if present, use the specified name to map to the database column. If the attribute is not present, it would fall back to the current behavior of using the field's name directly.
Example:
Let's say we have a database table users with a column named user_id. In our application code, we would prefer to use id for better readability. With the proposed attribute, we could define our model like this:
struct User {
#[key]
#[column("user_id")] // New attribute
id: i32,
#[column("user_name")]
name: String,
// Other fields...
}