Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow to rename table columns in table! macro and do it automatically for invalid names #967
Comments
This comment has been minimized.
This comment has been minimized.
|
The naive solution would be to add some form of "real name" as foo -> Integer, I consider Django ORM to be a golden standard in this regard because it incorporates
as well as properties specific for particular field type:
https://docs.djangoproject.com/en/1.11/ref/models/fields/ Perhaps not all of it makes sense for Diesel, but I'd prefer to have generic and extensible solution for setting field and table attributes. As for syntax, I'd suggest something similar to Django: table! {
users {
id -> Integer(primary_key: true),
name -> VarChar(db_column: "username", max_length:120),
properties:
indexes: ...
db_table: "user"
}
}(I find ":" more readable than "=") |
This comment has been minimized.
|
Let's keep the feature focused. The use case we want to support here is renames, we don't need to complicate things by adding a bunch of other information that Diesel does not care about (and likely never will). I'm more in favor of using attributes for this, but want to make sure the naming makes it very clear what is the Rust side and what is the SQL side. table! {
users {
id -> Integer,
#[database_name="username"]
name -> Text
}
}I'd also like to see a concrete proposal for the conventional renames for Rust keywords. |
This comment has been minimized.
I don't mind the format for setting attributes, but I'd prefer to call this attribute column_name or database_column_name - that makes clear what it does even for someone not familiar with diesel.
Django adds _field sufix in this case. Since Diesel uses column name, that would be _column. "mod" => column_mod I'd be happy if whole normalization (and adding comments to explain it) would be stolen from Django. |
This comment has been minimized.
|
I'd rather not establish a convention for column names containing only numbers (or any other column which is not valid as an unquoted identifier in SQL) |
This comment has been minimized.
|
You will need to have some convention, interact with user or generate code that doesn't compile (as it is now). Of those three, I'd definitely prefer convention. Interacting with database tool for some large database is not something I'd enjoy doing. I am working with a system that has dozens of databases and thousands of tables. I have no idea how many of those are not valid identifiers in Rust, but I do not want to go through all of them and make a decision how it should be renamed for every case. |
This comment has been minimized.
|
Why should we special case numbers but not spaces? Or other characters that aren't valid identifiers? We would have to conventionally handle any arbitrary sequence of characters. That said, the issue with Rust keywords (notably
Have you tried throwing |
This comment has been minimized.
My opinion is that Diesel should handle everything - I've linked to Django as it covers a lot of cases (I am not sure if all, but many more than just numbers and keywords).
The problem is that this can be extremely tedious and user-unfriendly work. There is no point in users doing it at all. |
This comment has been minimized.
I agree, which is why the overwhelming majority of the time, you won't see column names that aren't valid SQL identifiers. I don't see the value of adding a convention for breaking convention, especially one that's so uncommon. |
This comment has been minimized.
They do exist though, I can't ignore that, and I want the tools I am using to deal with that without involving me. I do not want for example to break my build scripts just because someone decided to add some column that Rust doesn't like. This is trivial problem to solve and I see no reason why a database tool should ever expose such issues to users. Perhaps look at this in context of creating web project as a whole, which includes using multiple tools. Preferably all of them should work as smoothly and automatically as it is possible. |
This comment has been minimized.
|
I'm not claiming that they don't exist, I'm saying that they're not common enough to warrant any sort of convention for automatically renaming them, especially since columns that are invalid SQL identifiers are by definition breaking convention. The issue is already going to be exposed to the user when they have to figure out what we renamed their column to. Anyway we're not getting anywhere by debating that further. I think you've made your position very clear. What I'd like to move forward with for the time being is an annotation to allow renames (something like |
This comment has been minimized.
|
Great, thanks. |
killercup
referenced this issue
Jul 11, 2017
Closed
Naming Collision When Column Called "Timestamp". #1018
This comment has been minimized.
|
Also interesting: Column names that collide with type names that are in scope: #1018 |
This comment has been minimized.
Measter
commented
Jul 11, 2017
|
Apologies for missing this issue; apparently my google-foo isn't as good as I'd hoped. In terms of re-name syntax, I'm not massively familiar with writing macros, but is it possible to do something like this:
|
This comment has been minimized.
|
@Measter It is possible, but I do not like that syntax. Which side is the rust side? Which is the SQL side? I'd strongly prefer something that uses an attribute (for consistency with Rust's syntax), and makes it clear in the name which side is which. |
This comment has been minimized.
|
@sgrif And I think it wouldn't look good with keywords (if it even works),
|
This comment has been minimized.
|
Agreed |
killercup
referenced this issue
Aug 9, 2017
Merged
Add a way to rename a column in the table! macro #1084
Eijebong
referenced this issue
Aug 16, 2017
Merged
Automatically rename columns which name would collide with a keyword #1110
This comment has been minimized.
Eijebong
closed this
Aug 24, 2017
This comment has been minimized.
|
I don't think this issue should have been closed. The initial poster specifies:
But the fix in #1084 only applies to column names. I also am interested in a fix that applies to table names as well. |
This comment has been minimized.
|
Can you open a new issue for renaming table names? |
Fiedzia commentedJun 26, 2017
I have a table with a column named "mod", which is not valid identifier in Rust.
Even for valid identifiers, I want to be able to use different name than what database provides,
is that possible?
Same thing should be applied to table names.