-
Notifications
You must be signed in to change notification settings - Fork 228
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
Automatic conversion from Int to ID is problematic #102
Comments
Note that PostgraphQL (Graphile) doesn't convert numeric primary keys to IDs either. Relay ids are provided as |
@Cito I agree with you.Also I think it's necessary to keep the consistence between model and the data responded to client. |
Bump, this would be usefull! |
Agree - please leave primary key datatypes alone. ID! makes sense for the relay interface but if not using that then unconditionally coercing primary key attributes to string is not beneficial. Trying out just always returning an Int instead of an ID in the function below for my use case and so far it seems fine.
|
@ddrouin how did you manage to revert this behavior? When I put this override in my code, it doesn't work - IDs are still returned as a base64 encoded string:
|
Any progress on this one? I use relay and I wonder if there is any workaround to have foreign keys converted automatically. |
Hi everyone, I'm interesting in pushing this over the finish line. We all agree here that the conversion should be consistent. The only thing left to decide is wether we want to convert all keys to IDs or completely opt out of this behavior. I'm personally in favor of converting all keys to IDs because it is more correct from a type perspective. The 'ID' type means that clients are not supposed to operate on in any way. For example, the generated Additionally, I don't think that we should not use Regarding the @Nabellaleen Any thoughts on that? Cheers, |
Any updates on this issue? |
I'm sharing the opinion of @Cito and @ddrouin: The columns from the database should be left untouched. I named my primary keys Just my two cents. As a beginner with graphene, I was lost initially. |
My understanding of this issue is that it's not what type foreign keys should be represented as but whether they should be added to the schema at all—in my opinion, they should not be a part of the schema. A node is an entity with static properties. A The foreign key field is just how we enforce the connection between a There's a great article that talks about this in more detail: Schema Design != … If you need to access the query {
users {
department {
id
}
}
} You can see an example of this in GitHub's GraphQL API. On the backend, it's a MySQL database where a repository surely has a foreign key query {
viewer {
login
id
repositories(first: 10) {
edges {
node {
id
# There's no `user_id` field here.
owner {
id # You access the `user.id` of the `owner` here.
}
}
}
}
}
} By converting foreign keys to |
I used this way to make sure the Integer primary key keeps as Integer:
|
FYI: With the recent code base change, I fix @JBrVJxsc's code a bit. from graphene_sqlalchemy.utils import column_type_eq
@convert_sqlalchemy_type.register(column_type_eq(SmallInteger))
@convert_sqlalchemy_type.register(column_type_eq(Integer))
def convert_column_to_int_or_id(type, column, registry=None):
return Int |
#379 extends the column to ID column behavior from |
Where do you put this function? I put it where the Schema is declared but it doesn't get called.. |
Did you get a solution for this @anh193 ? For me, there's no column_type_eq in graphene.sqlalchemy.utils |
Graphene-SQLAlchemy automatically converts columns of type SmallInteger or Integer to
ID!
fields if they are primary keys, but does not convert such columns toID
fields if they are foreign keys.Take for example this schema:
You can run the following query:
As a result, you get something like:
As you see,
department.id
is a string (because IDs are returned as strings), whiledepartmentId
is a number. This turned out to be a huge problem and source of error in practice. Working with this inconsistent, fault-prone interface has bitten me many times. When storing ids in objects on the frontend, or using ids as filters, I never know whether I should use numbers or strings. Currently I have conversions from number to string and vice versa everywhere in my frontend code, and if I don't do it correctly, things stop working in hard to debug ways because you often don't recognize such type mismatches. On the server side, do I take ids used as filter parameters as IDs or Ints? If I do the former, I must then convert them to integer when using them as filter arguments for SQLAlchemy. So, really, this is no fun to work with and doesn't work in practice, because you always have this mental burden of thinking about whether your ids should be represented as strings or numbers and whether you need to convert them when passing them around.I suggest the conversions should be consistent. Either convert all keys, including foreign keys, to IDs, or do not make a special case conversion for primary keys. Actually I'd prefer the latter, since then I never need to think about the type and since storing numbers on the frontend uses less memory.
Now of course I know that there is the relay specification which assumes there is an
id
field with a type ofID
. So when using the relay interface, things are different. In this case, I suggest converting to IDs everywhere (including foreign keys) - but here we need conversion of the values to global ids anyway, they are not just the row ids converted to strings.The text was updated successfully, but these errors were encountered: