Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MiroSk on June 10, 2015, 07:19:25 pm

Title: VBSCRIPT - how to set primary key?
Post by: MiroSk on June 10, 2015, 07:19:25 pm
I have vbscript, which creates new class (with stereotype  "table") in EA. The script adds attributes and its datatypes to the class - till this point the script works OK.
The problem is to add a primary key. To add primary key I am using sub AddPrimaryKey (see bellow, table = the class into which PK should be added, name = name of the new PK (new atribute), datatype = datatype for PK, note = note for PK).
When I checked the created class in the Project browser-> Class -> Table detail -> Columns/Attributes, the primary key checkbox is not checked.

Where is the problem? What should I do so that Primary key checkbox will be set?


sub AddPrimaryKey(table, name, datatype, note)
      dim newattributes as EA.Collection
      set newattributes = table.Attributes
      dim newAttribute as EA.Attribute
      set newAttribute = newattributes.AddNew( name, datatype )
      newAttribute.Stereotype = "column"
      newAttribute.AllowDuplicates = 1
      newAttribute.Notes = note
      newAttribute.Update()
      newattributes.Refresh
      
      dim newOperations as EA.Collection
      set newOperations = table.Methods
      dim newMethod as EA.Method
      set newMethod = newOperations.AddNew("PK_"+Mid(table.Name,4),"")
      newMethod.Stereotype="PK"
      newMethod.Update                              
      newOperations.Refresh
      
      dim parameters as EA.Collection
      set parameters = newMethod.Parameters
      dim newParameter as EA.Parameter
      set newParameter = parameters.AddNew( name, datatype )
      newParameter.Update()
      parameters.Refresh()
      newMethod.Update                              
      newOperations.Refresh      
end sub
Title: Re: VBSCRIPT - how to set primary key?
Post by: Geert Bellekens on June 10, 2015, 08:41:32 pm
Have you tried Attribute.IsID?

If that doesn't work the best strategy is to create one manually and then inspect the database.

Geert
Title: Re: VBSCRIPT - how to set primary key?
Post by: qwerty on June 10, 2015, 08:43:06 pm
This is really too long ago but you should create a simple empty model where you can verify it. Check what t_xref contains once you created a FK manually. Most likely this will contain the missing link.

q.
Title: Re: VBSCRIPT - how to set primary key?
Post by: MiroSk on June 11, 2015, 12:04:33 am
Thank you for the replies,  I am investigating data in EA system tables, but for now nothing help.
Title: Re: VBSCRIPT - how to set primary key?
Post by: Aaron B on June 11, 2015, 09:51:34 am
Doesn't look like you are setting Attribute.IsOrdered in your code.  Add the following line when creating your "column" attribute.

newAttribute.IsOrdered = 1
Title: Re: VBSCRIPT - how to set primary key?
Post by: MiroSk on June 11, 2015, 05:07:36 pm
Many thanks to Aaron B, newAttribute.IsOrdered = 1 solved my problem.