DEV Community

Cover image for Form Relations vs Table Relations: What's the Difference?
Jeff Tsai
Jeff Tsai

Posted on Originally published at hackmd.io

Form Relations vs Table Relations: What's the Difference?

A guide to a 4-minute video: it starts with one sentence from a user, "pick the salesperson, and their department comes along"

The video is about 4 minutes long, with English narration and English or Traditional Chinese subtitles on YouTube. It was recorded with Polhem 1.12.1.

The previous two posts walked through the whole flow: importing definitions from Excel, then running them on the desktop, the web and phones. This one is about a single topic: how relations between forms are designed. The demo continues with the Northwind project from the Blazor Server video and adds an employee and a department to the order.


1️⃣ One sentence from the user

Ask a user what an order needs, and you often hear a sentence like this:

Pick the salesperson, and their department comes along. If they move to another department later, this order stays with the old one.

That sentence is the whole requirement. The first half is about the action: pick a person, and the related data comes with them. The second half is about where the record belongs: the order keeps the department at the time it was made, and does not follow the employee's later moves.


2️⃣ The traditional way: translate the sentence into tables

In a typical system, a developer first translates the sentence into tables and foreign keys: orders.employee_id points to employees, and employees.dept_id points to departments. Then three things have to be written:

  • The query: the list shows the employee and department names, so it needs a join
  • The lookup window: so the user can pick an employee
  • The handling after a pick: code that copies the code, name and department into the order once an employee is chosen

The second half, "stays with the old department", needs its own thought too. The department has to be stored on the order. If it is joined through the employee every time, the old orders change department as soon as the employee moves.

All of this is done again on every screen that uses the employee. The requirement is one sentence, but the implementation is spread across SQL, screens and code, and reading the original requirement back out of the code later is not easy.


3️⃣ The Polhem way: forms refer to forms

In Polhem, relations are defined on the form (FormSchema, the definition file that describes which fields a form has). A relation says "this form refers to that form", not "this table's foreign key points to that table's primary key". What the field says is: when this record is picked, bring back these fields. That is the same thing the user said.

The video first opens DefineEditor (Polhem's definition file editor) to look at two existing forms:

  • The Employee form's department field refers to the Department form and brings back the department's code, name and manager
  • The Department form's manager field refers back to the Employee form

Each form only describes the next level it refers to directly. When a form needs a field from further down, the framework follows the chain and builds the multi-level joins itself, so whoever writes the definition only has to think one level at a time.


4️⃣ Writing the sentence on the order

Next, the video opens the Order form in DefineEditor, adds an employee field, and points it at the Employee form. The relation dialog has already ticked the employee's code and name, which are ordinary display fields.

For "their department comes along", the employee's department fields are ticked too and written into the order's own department fields, marked LookupOnly (applied only when picking): they are filled in when an employee is picked, then saved along with the order.

The department field itself is read-only, because it always comes from the employee and users cannot pick it on their own. It still refers to the Department form, so the order can show the department name and be searched by department.

After saving, the Order definition looks roughly like this:

<FormField FieldName="employee_rowid" Caption="Employee" DbType="Guid"
           RelationProgId="Employee" ValueChangeCallback="true">
  <RelationFieldMappings>
    <FieldMapping SourceField="sys_id"        DestinationField="ref_employee_id" />
    <FieldMapping SourceField="sys_name"      DestinationField="ref_employee_name" />
    <FieldMapping SourceField="dept_rowid"    DestinationField="dept_rowid"    LookupOnly="true" />
    <FieldMapping SourceField="ref_dept_id"   DestinationField="ref_dept_id"   LookupOnly="true" />
    <FieldMapping SourceField="ref_dept_name" DestinationField="ref_dept_name" LookupOnly="true" />
  </RelationFieldMappings>
</FormField>
<FormField FieldName="dept_rowid" Caption="Department" DbType="Guid"
           RelationProgId="Department" ReadOnly="true">
  <RelationFieldMappings>
    <FieldMapping SourceField="sys_id"   DestinationField="ref_dept_id" />
    <FieldMapping SourceField="sys_name" DestinationField="ref_dept_name" />
  </RelationFieldMappings>
</FormField>
Enter fullscreen mode Exit fullscreen mode

It reads like the user's sentence: the order belongs to this employee, and to their department. A few things are worth a second look:

  • RelationProgId="Employee" points to the Employee form, not the employees table.
  • The three LookupOnly mappings only write values into the order when an employee is picked. They take no part in the joins at query time. The order's department is read back through its own dept_rowid and the relation below it, so it is the department stored on the order, not the employee's current one. That distinction is what makes the second half, "stays with the old department", work.
  • ValueChangeCallback was ticked by the dialog automatically. The lookup window only returns the employee's code and name, not the department, so after a pick the server reads the employee's full record and fills the department in.

5️⃣ Sync and upgrade: no SQL, no code

Once the definition is done, Sync in DefineEditor (synchronizing the table definition and the form layout) adds the new fields to the table definition and the layout. Then Upgrade Database adds the columns to the order table. No SQL and no code are written along the way.

Opening the order's layout in the web preview shows the employee on the form, with the read-only department next to it.


6️⃣ Trying it in the Blazor Server site

Finally, the video tries it in the Blazor Server site:

  1. A new order now has an employee and a read-only department.
  2. Pick the employee Nancy Davolio, and the department Sales East comes in with her.
  3. Save. In the list, the order's employee is Nancy and its department is Sales East.
  4. In the employee form, move Nancy to Sales West.
  5. Open the earlier order. Its department is still Sales East, the department she was in when it was made.
  6. Create another order and pick Nancy. This time Sales West comes in.

The department comes in after picking an employee

Both halves of the user's sentence show their results here.


7️⃣ Scope

This approach covers the everyday work on forms: creating, querying and editing records. Reports, statistics and batch jobs need SQL of any shape, and that is still written by hand in business objects. Relations have limits too: a join is always a single-column equality (foreign key to primary key), the target is a physical table, and subqueries and composite keys are not supported. The "When to Use / Not Use" and "Limitations and Design Tradeoffs" sections of the documentation list them.


✅ Wrapping up

With table relations, a developer translates the user's words into joins, lookup windows and code that copies fields. With form relations, the user's words are written directly as the definition. The benefit is that the requirement and the implementation line up: open the Order definition later, and you can read "pick an employee, bring back the department, keep the department with the order" straight from it, and every screen that uses it behaves the same way.

This is the approach I chose for Polhem, and it suits business systems with many forms whose fields and relations change often. If your system is mostly reports and complex queries, or you already manage your data model with an ORM, you may not need to switch. Judge it against your own project.

To try it yourself, install the templates with dotnet new install Polhem.Templates, then create a project with dotnet new polhem-blazor-server. The documentation and samples are on GitHub.

Further reading


📘 Original article on HackMD, in Traditional Chinese:
👉 https://hackmd.io/@jeff377/polhem-form-relations

📢 You are welcome to republish this, please credit the source.
📬 Follow for more engineering notes:
Facebook | HackMD | GitHub | NuGet

Top comments (0)