Skip to content
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

Working with auto increment IDs #80

Open
Slesa opened this issue Feb 1, 2023 · 3 comments
Open

Working with auto increment IDs #80

Slesa opened this issue Feb 1, 2023 · 3 comments

Comments

@Slesa
Copy link

Slesa commented Feb 1, 2023

Consider an order with an auto ID field, which can have multiple orderlines.

type Order = { Id: int64 option }
type OrderLine = { Id: int64 option; order: int64 }

Is there an example how to get the value of this generated ID in order to use it as the reference in the orderlines?
I know this can be done manually with

INSERT INTO Order(...) values(...) SELECT SCOPE_IDENTITY()

or "; SELECT last_insert_rowid()" for MSSql/Postgres

but maybe there is a more comfortable way?

And probably there is no other way than keep the ID as option, isn't it?

@JordanMarr
Copy link
Contributor

Your primary key fields should not be optional.
I don’t think any database even allow setting a nullable field as the PK.

Use InsertOutputAsync to return the generated id field value.

@Dzoukr
Copy link
Owner

Dzoukr commented Feb 2, 2023

I see this part is missing in the documentation so if you would @Slesa send a PR with some description for *OutputAsync methods, it may help others for next time.

@Slesa
Copy link
Author

Slesa commented Feb 6, 2023

Well I would like to, but... maybe it is because of monday, or I am too slow in thinking in general :)
When I use Order with Id set, I get the error 'Can not set explicit value on identity'. When I use an OrderDto w/o Id field, I cannot persist as it is a table of Orders, not OrderDtos. And I could not access the Id field anyway...

PS: @JordanMarr is right, the Id field cannot be optional

I bet it has sth to do with the Persons.View.generate in the tests somehow...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants