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

Allow consumption of generated values beyond generated keys upon INSERT #673

Open
polyoxidonium opened this issue Oct 26, 2021 · 5 comments
Labels
type: enhancement A general enhancement

Comments

@polyoxidonium
Copy link

polyoxidonium commented Oct 26, 2021

I am migrating from h2 driver to mssql driver and have problem with returning the id after saving.
Problem happens also with writing foreign keys via custom write converters.
It first seemd like the id generation happens after r2dbc converters do their job but when i completely removed them and temporary adapted entities to not contain nested entities, I figured out that the generated ids are not returned at all.
How am I then supposed to gather generated id if not immediately after creation?
This all worked just fine with h2 with uuid() function in the flyway script
however with the MSSQL NEWID() function i keep getting null, however in the database values are persisted.

Flyway:

create table Foo
(
    foo_id                  uniqueidentifier default NEWID() PRIMARY KEY,
)

Repository: public interface FooRepository extends ReactiveCrudRepository<Foo, String>{}

Entity:

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Table
public class Foo {

    @Id
    @Column(FooTable.ID) // foo_id
    private String id;

And so in the pipeline it looks like this:

    return fooRepository.save(foo)
        .doOnNext(f -> System.out.println(f.getId()); // prints null 

Not sure if it is a bug or I lack something.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 26, 2021
@mp911de
Copy link
Member

mp911de commented Oct 27, 2021

The SQL server driver returns only identifiers for IDENTITY columns. Default values are not supported.

@polyoxidonium
Copy link
Author

The SQL server driver returns only identifiers for IDENTITY columns. Default values are not supported.

So in order to use UUIDs, one has to programatically generate them before persisting?

@mp911de
Copy link
Member

mp911de commented Oct 27, 2021

Yes and beyond that, for every DEFAULT value/function that you want to consume, you need to fetch the persisted row. T-SQL supports an OUTPUT clause that can be used with INSERT but that has to happen from within the caller which is Spring Data R2DBC.

@polyoxidonium
Copy link
Author

Yes and beyond that, for every DEFAULT value/function that you want to consume, you need to fetch the persisted row. T-SQL supports an OUTPUT clause that can be used with INSERT but that has to happen from within the caller which is Spring Data R2DBC.

I see, I guess to stay simple I will just avoid default values in the database itself,
thank you!

@mp911de
Copy link
Member

mp911de commented Oct 27, 2021

Let's keep this issue open as other databases (e.g. Postgres) can automatically echo the entire inserted row without additional interaction. It would be neat if Spring Data could benefit from such behavior across multiple databases.

@mp911de mp911de changed the title ID not returned after ReactiveCrudRepository.save(...) Allow consumption of generated values beyond generated keys upon INSERT Feb 14, 2022
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants