Retrieving default data values for inserted record

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
sdaberle
Posts: 8
Joined: Tue 12 Sep 2017 16:34

Retrieving default data values for inserted record

Post by sdaberle » Thu 21 Oct 2021 21:16

I have MySQL tables with default values specified for some fields. When I connect to these tables with a TMyQuery or TMyTable and insert a new row, those fields show as empty. They only show the default values after I post the inserted record. I want to show the default values for these fields when the record is newly inserted. How can I do this?

I have tried setting the dataset's Options.DefaultValues property to True, which works for some fields but introduces another problem: I have a field, UserName, whose default value is current_user(). If I set DefaultValues to true, that field winds up being populated with the string 'current_user' instead of with the *value* for current_user(). How can I both populate and show the default values for the newly inserted fields, and get the correct value for current_user() (and, for that matter, for any other fields who's default is calculated based on a system variable)?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Retrieving default data values for inserted record

Post by ViktorV » Fri 29 Oct 2021 07:36

Hi!

Thank you for contacting Devart and for your inquiry!

If the TMyQuery.Options.DefaultValues property is set to True - MyDAC will set the DefultExpression property to the values obtained from the server.
To get the value, MyDAC queries the MySQL system tables, and in your case, the server will send the value current_user ().
Thus, this is the correct behavior of MyDAC.
To get the value of the field returned from the server after invoking a new record, you should execute the Refresh or RefreshRecord method.
Also, you can set the TMyQuery.RefreshOptions property to roAfterInsert to get actual data after insert new record:
https://www.devart.com/mydac/docs/devar ... ptions.htm

Please, let us know if you have any questions.

Post Reply