-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
table
macro that allows remapping table and field names
#424
Conversation
So I'm not entirely opposed to this feature, and would like to hear what @diesel-rs/core have to say on it. However I'm against it personally. I think it adds unnecessary complexity for a simple problem. The only use case where this is necessary are to have column names which are the same as keywords in Rust. This is a relatively limited set, and I think we can avoid the complexity simply by having a defined convention in those cases (e.g. |
Even if it is a small set of protected words, there are cases (e.g., working with a legacy database) where you can't avoid them. There are other cases (though along the same legacy line) where you might want a re-mapping even when not strictly required (e.g., non-underscored field name to snakecase). As I noted on #342, I think having a convention for Also, how would the convention work when going in the table! {
objects {
ty -> Nullable<VarChar>
}
} map to a field named "type" or "ty"? |
Ok so I've put some more thought into this. I like the idea but I don't like the API. It's not clear to immediately which side is the rust name and which side is the SQL name. |
I think this would be useful to have. I don't like the As there is currently no way in a stable macro to match on How about table! {
objects
(table "weirdly_long_table_name_for_something_as_simple_as_objects")
(id)
{
id -> Uuid,
label -> Nullable<VarChar>,
ty (column "type") -> Nullable<VarChar>,
created_at -> Timestamp,
updated_at -> Timestamp,
}
} |
@killercup I think your suggestion is good (especially the more verbose one), but what I was thinking (after my initial PR but before your comment) was the serde-like table! {
objects {
id -> Uuid,
[rename="type"]
type_ -> Nullable<VarChar>
}
} or like: table! {
objects {
id -> Uuid,
type_ [rename="type"] -> Nullable<VarChar>
}
} or using |
@kardeiz Why not use I think is actually more confusing—because we are in a macro context where there are no items that can have actual attributes (as seen by rustc). If the macro was using struct syntax (basically |
I still don't like rename as the API. It's unclear to me which side is being renamed. |
I do like the way serde solves this: https://github.com/serde-rs/serde/blob/4a0bf4de65b7787088c6c6ce3cc3b69fc1cc1a16/serde_codegen_internals/src/attr.rs#L109 basically it would look like this:
|
So we discussed this feature a bit more internally. The consensus was that there's not really a strong motivation for this feature in terms of general renaming of columns. Without a stronger motivation, we want to avoid introducing new features like this. What we do need to address are the table/column names that cannot be represented in Rust because they overlap with keywords. ( |
Forgot to mention -- part of the reason for not wanting to introduce this as an API in the |
Is it even possible to create a new ident Or (as noted above) if you are requiring the user to provide the In any case, I think I fundamentally disagree with this, and it seems like renaming Rust keywords would add more complexity than this PR (or something similar). As a potential Diesel user I would prefer to be required to do some configuration rather than work with some relatively arbitrary convention (appending underscore to reserved name). However, I do agree that the rename-on-use workaround would be acceptable for most cases. In any case, thanks for considering and for the explanations— |
I'm a newbie to Diesel, but is there any recommended workaround for having columns named I'm trying to connect to just a specific table to avoid that problematic column by using
|
It is necessary when working with existing databases. The table names are often out of touch with reality (or reality of specific application), but cannot be changed, they may be inconsistent and we want consistency in codebase, or were automatically generated and we want human-friendly names for them in our app.
This is a) horribly ugly and b) involves looking at (and changing) two places instead of one, while every single ORM I've ever worked with this was always stated once at the place of model definition, Therefore it has the horrible feature of surprising users. |
This PR is mostly meant as a starting point for resolving #342 (using table or field names that are reserved keywords in Rust). I didn't think too hard about how to do the field/table name specification. The macro can still be used as before.
If nothing else, this may be helpful as a starting point for others who have to work with tables that have fields like
type
.You can use the macro like: