-
Notifications
You must be signed in to change notification settings - Fork 6
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
Panics on different postgres types and custom types #9
Comments
@jayy-lmao postgres types can be added with no issues, how can we have custom enums generated ? |
I've mostly aimed to support the types listed in https://docs.rs/sqlx/latest/sqlx/postgres/types/index.html. Custom enums seem like something we may want to add, but it will be a new feature and may take some work. Some of these types with the underscore prefix, are you referring to that type without the underscore or is the underscore prefix common in Postgres? I see it's used for internal types, and not sure if sqlx will support it. [Update: It looks like this may be because you're trying to use it with arrays, which also may need to be added] |
I will add a naive support for arrays, which hopefully addresses these |
I have been working on a project which needs sql-gen, maybe I can help with fixing the errors due to custom enums. Regarding, the internal types, the above mentioned ones are some I encountered while working on the project, I'll make a pr for their mappings with appropriate rust types. If I find any else type panicking, I will update. |
Any idea how we can get started with supporting custom types? |
I have merged the PR that allows for array type, time, and character types. Let me know if it has resolved any of the above.
The only custom type I can see supported by sqlx is enum type. I would check if there's a way to query postgres for enums and their options, then map those to models with their own model files. Then you would maybe need a list of which ones you had done this for, so that when a table references them, you can map it to a type on the struct. It might be a bit of work. |
@yellowHatpro here is a query you may wish to use SELECT
n.nspname AS schema_name,
t.typname AS enum_name,
e.enumlabel AS enum_option
FROM
pg_type t
JOIN
pg_enum e ON t.oid = e.enumtypid
JOIN
pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE
t.typtype = 'e'
ORDER BY
n.nspname,
t.typname,
e.enumsortorder; To reiterate I think: Then later we may want to |
Thanks, that was really helpful! |
Hi @jayy-lmao , I tried with your query and it worked for custom enums ✅✅. pub fn convert_type(data_type: &str,
custom_types: &Vec<String>) -> String {
if custom_types.contains(&data_type.to_string()) {
return to_pascal_case(data_type);
} else {
convert_data_type(data_type)
}
} However, now the error is popping on cube, which I think is a postgres extension. Is there any way to include cube (or any available extension present) in the sql query you highlighted? |
Looking good! If that's the case maybe in addition to the enum support we might need two changes:
Either something like
Or maybe more based on the table,field
Either or both could be environment variables, and accept multiple values. Let me know if you see / know how to support cube with sqlx 👍 |
Right, I had a look couple months back regarding sqlx cube support, didn't find anything relevant. |
I had a bit of investigate - and no support. |
That's awesome! Thanks for the help !! 😀😀 |
Problem
"_int8" => "i64",
"_int4" => "i32",
"_int2" => "i16",
"_text" => "String",
"jsonb" => "serde_json::Value",
"time" => "chrono::NaiveTime",
"bool" => "bool",
"bpchar" => "String",
"char" => "String",
"character" => "String",
The text was updated successfully, but these errors were encountered: