Page 1 of 1

Retrieving default data values for inserted record

Posted: Thu 21 Oct 2021 21:16
by sdaberle
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)?

Re: Retrieving default data values for inserted record

Posted: Fri 29 Oct 2021 07:36
by ViktorV
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.