Foreign Key Implementation

The way foreign keys are stored in your Schema Dictionary after a database import or AI code generation directly impacts the application's code. Missing foreign key references, for example, will result in generated code that is incomplete or incorrect, leading to potential runtime issues. Properly documenting and maintaining these foreign key relationships ensures the generated code is accurate and reliable. If foreign keys are not correctly captured or omitted, the application may encounter compile errors or data integrity problems.

Different indicators in your Schema Dictionary can point to potential issues. This can help you identify what needs to be fixed before you start generating your code.

Self-Referencing Tables

The visual representation in the Schema Dictionary looks like this:

self-referencing table (also called a self-join table) is a table in a relational database where a foreign key in the table references the primary key of the same table. For example, in an Employee table, each employee may have a manager who is also an employee. 

Table: Employee

EmployeeID Name ManagerID
1 Alice NULL
2 Bob 1
3 Charlie 1
4 Dave 2
  • EmployeeID Is the primary key

  • ManagerID is a foreign key that references EmployeeID in the same table

Please confirm that the stencil you are using supports this type of relationship. By default, any column with this type of assignment will be excluded from code generation.


Missing Display Column

The visual representation in the Schema Dictionary looks like this:

During a database import, CodeStencil tries to choose the best columns to display for Foreign Keys automatically. Sometimes, it can't pick the right column, so you may need to select the display column for your foreign key manually. You can do this opening the column edit form in any of these ways:

  1. Double click on the selected row;
  2. Click the CHANGE button while the row is selected;
  3. Using the context menu. Right-click on the selected row, and choose CHANGE.

Next, select the display column you would like to use:

 

 

 

 


Was this article helpful?