AL icon indicating copy to clipboard operation
AL copied to clipboard

Increasing Field Length in TableExtension Causes SQL Error During Schema Sync

Open jcooper01 opened this issue 8 years ago • 7 comments

We have a table that is a child table of a native Microsoft table (Item Category). Therefore, the Item Category Code is in our primary key. The Item Category Code has been increased to a Code20 from a Code10. We need to increase the length of the Item Category Code field in our table, but when we do that in a v2 extension, we get the following error during Sync-NAVApp: Sync-NAVApp : The following SQL error was unexpected: Incorrect syntax near 'USA'. It's generating the following SQL: The following SQL error was unexpected: Incorrect syntax near 'USA'. SQL statement: DECLARE @StatisticsToDrop NVARCHAR(MAX); SELECT @StatisticsToDrop = COALESCE(@StatisticsToDrop +', ', '') + '[CRONUS USA, Inc_$Product Group UoM -CL-$1318c399-02ab-425c-81ce-19ff5230d815].'+ s.name FROM sys.stats AS s INNER JOIN sys.stats_columns AS sc ON (s.stats_id = sc.stats_id AND sc.object_id = s.object_id) INNER JOIN sys.all_columns AS c ON (c.column_id = sc.column_id AND c.object_id = s.object_id) WHERE s.object_id = OBJECT_ID('CRONUS USA, Inc_$Product Group UoM -CL-$1318c399-02ab-425c-81ce-19ff5230d815') AND (c.name = 'Item Category Code' ) IF @@ROWCOUNT > 0 BEGIN SET @StatisticsToDrop = CONCAT('drop statistics ', @StatisticsToDrop) EXECUTE sp_executesql @StatisticsToDrop END

How should we handle this situation?

jcooper01 avatar May 07 '18 19:05 jcooper01

same issue with the custom entity. primary field length increased from 'code 10' to 'code 20' and I have the same error:

The following SQL error was unexpected: Incorrect syntax near 'A$Entity'. SQL statement: DECLARE @StatisticsToDrop NVARCHAR(MAX); SELECT @StatisticsToDrop = COALESCE(@StatisticsToDrop +', ', '') + '[Company A$Entity Group].'+ s.name FROM sys.stats AS s INNER JOIN sys.stats_columns AS sc ON (s.stats_id = sc.stats_id AND sc.object_id = s.object_id) INNER JOIN sys.all_columns AS c ON (c.column_id = sc.column_id AND c.object_id = s.object_id) WHERE s.object_id = OBJECT_ID('Company A$Entity Group') AND (c.name = 'Code' ) IF @@ROWCOUNT > 0 BEGIN SET @StatisticsToDrop = CONCAT('drop statistics ', @StatisticsToDrop) EXECUTE sp_executesql @StatisticsToDrop END

yandko avatar May 09 '18 16:05 yandko

Bug in the platform that has now been fixed. This (changing the length of any PK field) should be banned and should fail sync earlier with an appropriate validation message.

cmrudolph-msft avatar May 11 '18 13:05 cmrudolph-msft

OK, so how can we increase the length of a PK field (which we currently need to do for Item Category Code)?

jcooper01 avatar May 11 '18 14:05 jcooper01

The only story around PK changes currently looks like:

  1. Create a new table
  2. Mark the old table as obsoleted (ObsoleteState = Pending is the best you can do in an extension for now. In the future the Removed state will be supported with runtime enforcement if accessed outside upgrade code)
  3. Copy the data from the old to new table in upgrade code
  4. Make sure everything points at the new table

cmrudolph-msft avatar May 11 '18 17:05 cmrudolph-msft

I'm not sure that's a sustainable solution. There have been many instances of Microsoft increasing a field length (Item Category Code is just one -- External Document No. also increased relatively recently). We aren't trying to change the PK fields or shorten a PK field. Are you sure that there won't be the ability to lengthen a PK field in the near future?

jcooper01 avatar May 11 '18 17:05 jcooper01

I agree it's not sustainable (or ideal) and we are having a discussion about this internally.

The PK lengthening restriction is not arbitrary. It stems from the use of PK as the link between main tables and companion tables. As a result, a PK change is potentially detrimental to any upstream dependents (which cannot be known about in advance or controlled thanks to per-tenant customizations in BC).

Some rough ideas to improve this include:

  1. Better controls on which parts of an extension are extensible and which parts are closed off (think sealed/virtual as an OO analogy). This would allow certain parts of an extension to change more freely knowing there is no chance of impacting things like companion tables (since none can exist). This concept could be extended beyond "am I extensible" to include something of a public/private distinction too. The net result would be a more clear sense which parts of an extension are for consumption by others and which things are pure internals (the latter being much more free to change).

  2. An alternative linking strategy not based on PK. Some form of a surrogate key makes sense here and brings utility to other places in the application.

  3. Sticking with the PK linkage but introducing functionality to propagate length changes to existing companion tables (transparently during the sync process).

cmrudolph-msft avatar May 11 '18 18:05 cmrudolph-msft

Not that it matters, but my vote would be option 3. It makes sense that when there is a PK/PK relationship (like with a child table) that the fields match up. I guess there could be some complexity if there was a conditional table relationship, but conditional relationships on a PK are somewhat more rare than simple parent/child relationships.

jcooper01 avatar May 11 '18 18:05 jcooper01