Book a Demo

Author Topic: VBSCRIPT - how to set primary key?  (Read 7287 times)

MiroSk

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
VBSCRIPT - how to set primary key?
« 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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBSCRIPT - how to set primary key?
« Reply #1 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

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: VBSCRIPT - how to set primary key?
« Reply #2 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.

MiroSk

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: VBSCRIPT - how to set primary key?
« Reply #3 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.

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: VBSCRIPT - how to set primary key?
« Reply #4 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

MiroSk

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: VBSCRIPT - how to set primary key?
« Reply #5 on: June 11, 2015, 05:07:36 pm »
Many thanks to Aaron B, newAttribute.IsOrdered = 1 solved my problem.