Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread Indexes deleted on altertable?
Mon, Mar 10 2014 6:34 PMPermanent Link

F. van Daalen

Version 4.37 Build 3

Adding a field to a table and then adding a an index will delete all indexes?

           with FieldDefs do
           begin
              update;
              if IndexOf('Norm_Id')=-1 then
              Begin
                 Altered :=true;
                 Add('Norm_Id',ftLargeInt);
              End;
           end;
           if Altered then altertable;

           With IndexDefs do
           Begin
              update;
              If IndexOf('ByNorm')=-1 then
              Begin
                 Add('ByNorm','Norm_Id',[],'',icNone);
                 Altered := true;
              end;
           end;
           If Altered then AlterTable();

after this all indexes except the newly created one are deleted?
Mon, Mar 10 2014 6:42 PMPermanent Link

F. van Daalen

PS Not doing the altertable after adding the field generate an error when generating the index as the field doesn't exist yet?
Mon, Mar 10 2014 7:13 PMPermanent Link

F. van Daalen

Ok the workaround is to add an IndexDefs.Update; statement BEFORE adding the field. Then after the field is added call altertable and then the index can be added.....

pwhuhhh
Mon, Mar 10 2014 7:45 PMPermanent Link

Raul

Team Elevate Team Elevate

On 3/10/2014 7:13 PM, F. van Daalen wrote:
> Ok the workaround is to add an IndexDefs.Update; statement BEFORE adding the field. Then after the field is added call altertable and then the index can be added.....

This is not a workaround - this is by design and always has been. You
must call Update on both FieldDefs and IndexDefs if you wish to retain them.

Altertable has no way of know what you wish to alter so it always alters
both.

Raul
Tue, Mar 11 2014 4:42 AMPermanent Link

F. van Daalen

Raul wrote:

This is not a workaround - this is by design and always has been. You
must call Update on both FieldDefs and IndexDefs if you wish to retain them.

Altertable has no way of know what you wish to alter so it always alters
both.

Raul

If it's by design then it's inconsistent by design. When adding only an index without the fieldsdef.update then after the altertabel the fields aren't deleted.
Tue, Mar 11 2014 4:51 AMPermanent Link

F. van Daalen

If it's by design then it's inconsistent by design. When adding only an index without the fieldsdef.update then after the altertabel the fields aren't deleted.

or add only fields without an index and without the indexdefs.update will also not delete any indexes.

So only when doing both you must first do a indexdefs.update before touching the fields.
Wed, Mar 19 2014 9:35 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frans,

You don't need two AlterTable calls here, just one:

           FieldDefs.Update;
           IndexDefs.Update;

           with FieldDefs do
           begin
              if IndexOf('Norm_Id')=-1 then
              Begin
                 Altered :=true;
                 Add('Norm_Id',ftLargeInt);
              End;
           end;

           With IndexDefs do
           Begin
              If IndexOf('ByNorm')=-1 then
              Begin
                 Add('ByNorm','Norm_Id',[],'',icNone);
                 Altered := true;
              end;
           end;
           If Altered then AlterTable();

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Mar 19 2014 9:38 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frans,

<< Ok the workaround is to add an IndexDefs.Update; statement BEFORE adding
the field. Then after the field is added call altertable and then the index
can be added..... >>

See the example in my reply to your original post.  You're just approaching
the alteration in the wrong way - the alteration is a *table-wide*
operation, meaning that all structure information (FieldDefs and IndexDefs)
must be populated before calling AlterTable, whether that is done manually
or automatically via the relevant Update calls.  Any structure information
not present at the time of the AlterTable call will be removed by DBISAM in
the actual table.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Mar 19 2014 9:41 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Frans,

<< If it's by design then it's inconsistent by design. When adding only an
index without the fieldsdef.update then after the altertabel the fields
aren't deleted. >>

How are you "adding the index" ?  If you call IndexDefs.Update, DBISAM
automatically calls FieldDefs.Update because it needs the field definitions
in order to process the index definitions.  This is *not* the case with the
field definitions, nor would it make sense to have FieldDefs.Update
automatically update the index definitions.

Tim Young
Elevate Software
www.elevatesoft.com
Image