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 upAllowing a type that's Queryable and Insertable to autoincrement its ID #1440
Comments
This comment has been minimized.
|
Ah, I just found this section on
I'll do this for now, but this is a little suboptimal given the amount of overlap you're going to have between the insertable and queryable versions for most models — models tend to balloon to lots and lots of fields given enough time, and all of them will need to be written twice (although I suppose the approach has some advantages as well). |
This comment has been minimized.
This is what I like to call "incidental duplication" -- which is the term I use to refer to things that happen to be structurally similar right now, but change in the future for different reasons. The approach we've taken is very specifically designed to separate things that represent the results of queries from things that represent input from external sources. The desire to keep these separate comes from experience with hundreds of applications. You can see similar approaches in other modern frameworks like Phoenix. If you really want to share these structs, struct UserWithId {
id: i32,
user: User,
}It sounds like you've found the answers you're looking for, so I'm going to close this issue. |
sgrif
closed this
Dec 30, 2017
This comment has been minimized.
anilanar
commented
Apr 9, 2018
|
@sgrif What do you mean by:
When I have the following:
|
This comment has been minimized.
|
@anilanar You need to add an explicit select clause to your query to transform the return type into the right one. users::table.select((users::id, (users::age,))).load::<UserWithId>(&conn);Should work. A other solution would be to manually implement |
This comment has been minimized.
anilanar
commented
Apr 9, 2018
|
It does work, but then it doesn't generate |
brandur commentedDec 29, 2017
•
edited
Setup
Versions
Problem Description
I'm running into a problem whereby I'm not sure how to make a type both
QueryableandInsertablewhile still having it autoincrement its primary key on inserts.I have a Postgres table with a
BIGSERIALtype like so:In my generated schema, this translates to something like this as you'd expect:
And finally, I have a model in my code that I've marked as
InsertableandQueryable:I'm trying to figure out how I'd use this setup to insert a new record while delegating ID generation to the database. The SQL for this type of operation looks roughly like this:
Or like this:
If my
idfield is a simplei64, there's no valid value that will make the database autoincrement —0,-1etc. are all insertableBIGINTs and are inserted literally. As far as I know, only omitting the field or using the specialDEFAULTvalue will do.I tried changing my
idto anOption<i64>:But then constraints on
Queryablerightly kick in and tell me I'm not allowed to do this:Any advice? Thanks!
(This looks a little like #640, but I don't think it's the same.)