Author Topic: How to get Foreign Key Columns using Script?  (Read 910 times)

patrik.slechta

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
How to get Foreign Key Columns using Script?
« on: August 09, 2023, 06:45:12 pm »
Dear All,
I am trying to extract structure of DataModel, so: tables, columns, primary key, foreign keys from EA Repository to JSON metadata file (to be used in another software outside EA).
Tables, columns, primary keys are quite clear but I feel stuck for days with Foreign Key detailed information. Especially the "Involved Columns" displayed on "Foreign Key Constraint" EA screen.

Foreign Keys itself seems to be defined in EA Repo following two places:
- as EA.Method stores in myTable.MethodsEx where StereotypeEx == "FK", providing simple list of Foreign Keys
- as EA.Connector stored in myTable.Connectors where Type == "Association" providing additional handy FK details

The Connector object contains many information for example:
- ForeignKeyInformation - contains DST=PK_name:SRC=FK_name:
- ClientEnd.Role - containt FK name separatelly
- SupplierEnd.Role - containt PK name separatelly
- SupplierEnd.Cardinality - contains Source Cardinality: 1
- ClientEnd.Cardinality - contains Target Cardinality: 0..*

Where the problem is?
I am stil unable to find actual columns (list of columns), which are used as part of the defined Foreign Key.
I am missing or overlooking something in the repositoy?


It would be nice if you could share some experience or inspire my next steps, please!  ;D
Kind Regards,
Patrik

patrik.slechta

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: How to get Foreign Key Columns using Script?
« Reply #1 on: August 10, 2023, 01:10:34 am »
As my exploration (just nicer name for troubleshooting) continued, there are some fresh findings.
Sharing it here, because somnebody can use it too...

Looks like the columns participating on the Foreign Key are stored in Repo as:
- myTable.MethodsEx.Parameters
- there is list of column names (Name) and colum position (Position) in defined Foreign Key

It is quite complex way of storing Foreign Key definition with information scattered between:
Method - there are Primary Keys and Foreign Keys defined for the table
Method.Parameters - there are Primary Key and Foreign Key participating columns stored
Connection - there are links between Foreign Key and refferenced Primary Key stored

So the FK columns are stored the same way as PK columns.
I have been just misled by all the other FK details present in paralell structure of Connection object :D

Richard Freggi

  • EA User
  • **
  • Posts: 473
  • Karma: +18/-7
    • View Profile
Re: How to get Foreign Key Columns using Script?
« Reply #2 on: August 10, 2023, 02:17:20 pm »
You may find it easier to forward engineer your EA data model to some DBMS, then reverse engineer from that DBMS to the other tool

patrik.slechta

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: How to get Foreign Key Columns using Script?
« Reply #3 on: August 11, 2023, 03:24:42 am »
Definitelly if used occasionally. We are under some transition period and the exchange of current data model structures might be qute frequent (due to the ongoing changes and development) for limited time.
It took few days of exploration and troubleshooting, but currently the import/export to/from JSON of tables, columns, datatypes, comments, primary keys, foreign keys... almost everything works fine.
It is not the state of the art solution but it works sufficiently  ;D

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 12654
  • Karma: +523/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to get Foreign Key Columns using Script?
« Reply #4 on: August 22, 2023, 06:55:18 pm »
Looks like you pretty much found all the bits and pieces. I feel your pain. I had to go through the same thing a few years ago myself :)

Geert

patrik.slechta

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: How to get Foreign Key Columns using Script?
« Reply #5 on: August 22, 2023, 11:38:05 pm »
Thank you Geert,
your EA related scripts, tutorials and responses here in the forum have been huge support on my suffering fascinating exploration :)
Patrik