Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2010-05

Should TAutoIncField be read only?

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luiz Americo Pereira Camara

Until a few moments ago i would say yes because it seems logical and fpc raises an exception when trying to set the value programatically.

But while investigating why TAutoIncField.ReadOnly always returns false, i found that, regardless of FReadOnly being set to true in the constructor, this value is overriden in TFieldDef.CreateField throung Attributes.faReadOnly flag.

So i tested in Delphi (TClientDatset and TDbf) and found that TAutoIncField.ReadOnly is false (like fpc).

So what should fpc do:
- Follow delphi behavior and let TAutoIncField.ReadOnly return False?
- Fix TAutoIncField.ReadOnly to return True?

Luiz

Reply by email

Michael Van Canneyt
to Luiz Americo Pereira Camara
Luiz Americo Pereira Camara wrote 14 lines
Until a few moments ago i would say yes because it seems logical and fpc raises an exception when trying to set the value programatically. But while investigating why TAutoIncField.ReadOnly always returns false, i found that, regardless of FReadOnly being set to true in the constructor, this value is overriden in TFieldDef.CreateField throung Attributes.faReadOnly flag. So i tested in Delphi (TClientDatset and TDbf) and found that TAutoIncField.ReadOnly is false (like fpc). So what should fpc do: - Follow delphi behavior and let TAutoIncField.ReadOnly return False? - Fix TAutoIncField.ReadOnly to return True?

As far as I know, you can force a value for an autoinc field, so it is not always read-only. AutoInc means the engine will assign a value if none is .

Reply by email

Joost van der Sluis
to Luiz Americo Pereira Camara: Until a few moments ago i would say yes because it seems logical and fpc raises an…
Luiz Americo Pereira Camara wrote 14 lines
Until a few moments ago i would say yes because it seems logical and fpc raises an exception when trying to set the value programatically. But while investigating why TAutoIncField.ReadOnly always returns false, i found that, regardless of FReadOnly being set to true in the constructor, this value is overriden in TFieldDef.CreateField throung Attributes.faReadOnly flag. So i tested in Delphi (TClientDatset and TDbf) and found that TAutoIncField.ReadOnly is false (like fpc). So what should fpc do: - Follow delphi behavior and let TAutoIncField.ReadOnly return False? - Fix TAutoIncField.ReadOnly to return True?

That's not fixing it, that's breaking it. Readonly have to be false for autoincremental fields.

One widely used method, for example, for using autoinc field and cached updates is setting the values of the autoinc fields to negative values when new records are created. That way you can make master-detail relations without having to apply all updates to the datasets. That's not possible anymore when you make those fields readonly.

Joost.

Reply by email

Luiz Americo Pereira Camara
to Michael Van Canneyt: As far as I know, you can force a value for an autoinc field, so it is not always…
Michael Van Canneyt wrote 22 lines
On Wed, 19 May 2010, Luiz Americo Pereira Camara wrote: > Until a few moments ago i would say yes because it seems logical and > fpc raises an exception when trying to set the value programatically. > > But while investigating why TAutoIncField.ReadOnly always returns > false, i found that, regardless of FReadOnly being set to true in the > constructor, this value is overriden in TFieldDef.CreateField throung > Attributes.faReadOnly flag. > > So i tested in Delphi (TClientDatset and TDbf) and found that > TAutoIncField.ReadOnly is false (like fpc). > > So what should fpc do: > - Follow delphi behavior and let TAutoIncField.ReadOnly return False? > - Fix TAutoIncField.ReadOnly to return True? As far as I know, you can force a value for an autoinc field, so it is not always read-only. AutoInc means the engine will assign a value if none is to set value through TField.AsInteger or TField.Value raises an

exception (autoinc fields are read only) both in Delphi and fpc.

Luiz

Reply by email

Luiz Americo Pereira Camara
to Joost van der Sluis: That's not fixing it, that's breaking it. Readonly have to be false for autoincremental…
Joost van der Sluis wrote 21 lines
On Wed, 2010-05-19 at 00:32 -0300, Luiz Americo Pereira Camara wrote: > Until a few moments ago i would say yes because it seems logical and fpc > raises an exception when trying to set the value programatically. > > But while investigating why TAutoIncField.ReadOnly always returns false, > i found that, regardless of FReadOnly being set to true in the > constructor, this value is overriden in TFieldDef.CreateField throung > Attributes.faReadOnly flag. > > So i tested in Delphi (TClientDatset and TDbf) and found that > TAutoIncField.ReadOnly is false (like fpc). > > So what should fpc do: > - Follow delphi behavior and let TAutoIncField.ReadOnly return False? > - Fix TAutoIncField.ReadOnly to return True? > That's not fixing it, that's breaking it. Readonly have to be false for autoincremental fields.

I'm just confused by the exception that is raised ("autoinc fields are read only") when trying to set a value and by the code in the TAutoIncField constructor that initializes FReadOnly to true.

Attached is a patch that removes this initialization.

Luiz

Reply by email

Joost van der Sluis
to Luiz Americo Pereira Camara
Luiz Americo Pereira Camara wrote 28 lines
Joost van der Sluis escreveu: > On Wed, 2010-05-19 at 00:32 -0300, Luiz Americo Pereira Camara wrote: > >> Until a few moments ago i would say yes because it seems logical and fpc >> raises an exception when trying to set the value programatically. >> >> But while investigating why TAutoIncField.ReadOnly always returns false, >> i found that, regardless of FReadOnly being set to true in the >> constructor, this value is overriden in TFieldDef.CreateField throung >> Attributes.faReadOnly flag. >> >> So i tested in Delphi (TClientDatset and TDbf) and found that >> TAutoIncField.ReadOnly is false (like fpc). >> >> So what should fpc do: >> - Follow delphi behavior and let TAutoIncField.ReadOnly return False? >> - Fix TAutoIncField.ReadOnly to return True? >> > > That's not fixing it, that's breaking it. Readonly have to be false for > autoincremental fields. > I'm just confused by the exception that is raised ("autoinc fields are read only") when trying to set a value and by the code in the TAutoIncField constructor that initializes FReadOnly to true. Attached is a patch that removes this initialization.

That's indeed strange. And it would mean that those fields are effectively readonly. I think they shoudn't be. (But to be sure, if it would have been my design, there woudn't be such a thing as an autoinc field.)

I see two choices: make it possible to edit autoinc fields, or make ReadOnly true.

Maybe we should choose the second option, for Delphi-compatibility?

Joost.

Reply by email

Luiz Americo Pereira Camara
to Joost van der Sluis
Joost van der Sluis wrote 20 lines
On Wed, 2010-05-19 at 14:14 -0300, Luiz Americo Pereira Camara wrote: > > I'm just confused by the exception that is raised ("autoinc fields are > read only") when trying to set a value and by the code in the > TAutoIncField constructor that initializes FReadOnly to true. > > Attached is a patch that removes this initialization. > That's indeed strange. And it would mean that those fields are effectively readonly. I think they shoudn't be. (But to be sure, if it would have been my design, there woudn't be such a thing as an autoinc field.) I see two choices: make it possible to edit autoinc fields, or make ReadOnly true. Maybe we should choose the second option, for Delphi-compatibility?

The funny thing is that, currently, fpc has the same behavior as Delphi, i.e., the field is effectively read only (fpc and Delphi raise the same exception when trying to set the value) but the ReadOnly property is false. Also, in both CanModify is true.

IMO this is a Delphi bug (Can someone test on > Delphi 7?). A quote from Delphi help:

property ReadOnly: Boolean;
Description
Set ReadOnly to true to prevent a field from being modified. In a data grid, tabbing from field to field skips over ReadOnly fields. To learn whether a field can be modified, use the CanModify property instead. CanModify reflects not only the value of the ReadOnly property, but also any restrictions imposed by the underlying dataset.

[AutoIncField]
Note: Auto-incrementing fields are a function of the database back-end. An application should not attempt to set a field value for an autoincrement field (with or without a TAutoIncField object).

Luiz

Reply by email