Before asking AI to implement a feature, what should you put in a sequence diagram, an ER diagram, or a class diagram?

April 17, 2025

Consider AssetMan, a simple asset management project. It registers equipment such as computers and records names, statuses, and locations. Each equipment type can also define its own fields, such as memory capacity for computers. Designing “create asset” means deciding how to store common and extra fields, how to make both writes succeed or fail together, and which application layer owns the writes. Before asking AI to implement the feature, I'd make those decisions explicit.

DiagramQuestion it answersDecisions worth marking
Sequence diagramDuring one operation, who calls whom first, and what happens on failure?Requests, permission checks, commit and rollback, and when a result is returned
Entity–relationship (ER) diagramWhich data should be stored separately, and how do they relate?Primary keys, linking fields, cardinality, and database-enforced constraints
Class diagramWhat is each object responsible for, and which interface does its caller depend on?Public methods, dependency direction, and interchangeable implementations

Sequence and class diagrams are UML. ER diagrams are for data modeling. When adding a feature to an existing project, I'd draw whichever layer is most likely to be misunderstood first. Existing SQL, interface signatures, and tests can also serve directly as implementation inputs.12

Draw the sequence far enough to test both success and failure

A sequence diagram arranges participants horizontally. Read the message order vertically. The Visual Paradigm tutorial says, “Time in a sequence diagram is all a about ordering, not duration.” Vertical distance here denotes order. For asset creation, the first thing I want to establish is whether the API can tell the frontend creation succeeded before the database confirms the commit.3

Donald Bell explains alt in IBM's tutorial: “Alternatives are used to designate a mutually exclusive choice between two or more message sequences.” I use it to show the two possible endings of the transaction. A break can mark where the request terminates early on failed authorization or a failed base record write. Each extra field triggers a write, so those writes go in a loop.4

Asset creation sequence. The API returns early on failed identity or permission checks. Inside the transaction it writes the base record and starts each extra field write. If all succeed or there are none, it commits and returns 201. A failed extra value causes rollback and a 500 response.

Redrawn from AssetMan's SQLite implementation. Authentication and authorization are combined in one step here; the controller code shows how commit failure is handled.5

Write the base record to obtain the asset ID, then use it to write the extra values. If there are extra fields, the controller issues each write and waits for all callbacks to finish before deciding whether to commit or roll back. Once that boundary is visible, we can specify an executable acceptance test. Make the second extra value fail and check that both the first extra value and the base record are undone. Asserting only that the API returned an error could miss a partial record left in the database.56

Use alt for two-way and multiway branches

The alt in the diagram has two compartments showing the messages for commit and rollback. Read it like an if/else. Use alt for a switch/case too. Give each case a compartment, put a guard in square brackets at its start, and add an [else] compartment for fallback behavior. In a batch import, for example, computers and monitors need different validation. Send unmatched rows to a review queue.4

Sequence for validation by asset type. An alt frame has three compartments: validate memory capacity for a computer, validate screen size for a monitor, or pass the current row to the review queue otherwise.

Sequence sketch for dispatching by asset type. It shows only the first message sent in each branch. Compare the flowchart for the same example.

If two compartment guards can both be true, UML's alt executes at most one, without specifying which one wins. Before handing the diagram to AI, I'd make the conditions mutually exclusive or specify on the diagram how to choose a unique outcome.4

Use the ER diagram to separate field definitions from field values

When building AssetMan I also consulted Snipe-IT, an open-source project for managing IT equipment and software licenses.7 To compare its table structure, see the Snipe-IT database relationship diagram on DrawSQL.8

AssetMan's early design separated the base asset record, extra values for an individual device, and a field template for an equipment type. These three boxes explain why the same field name appears in two places. In the template it describes a type, for example “memory capacity for a computer is numeric.” In the extra values it holds data for one particular computer, for example 32.9

Excerpt from AssetMan's early ER diagram retaining Asset_Base, Asset_Info, Asset_Type_Template, and the one-to-many link from the base record to extra values.

Partial redraw of the April 17, 2025 Draw.io original, preserving its fields, key annotations, and connections.9

The backend commit that same day already used an integer asset_id. The diagram's Asset_Info corresponds to asset_extra in code, and Asset_Type_Template to asset_type_template. The template table's primary key is (asset_type, asset_field_name). The extra value table's is (asset_id, asset_field_name). Neither table definition declares a foreign key. When asking AI to make incremental changes, I'd give it the current DDL and state whether the existing constraints should change. For example, “there can only be one extra field of the same name on the same asset” is already covered by a primary key. “The input field must exist in the selected type's template” still needs a decision about which layer checks it.1011

Draw collaborating code objects in a class diagram

If the goal is to let SQLite and SQL Server backends share the same asset creation call, the focus shifts to code responsibilities. Both AssetMan controllers expose createAsset(req, res). At startup, DB_TYPE selects the controller, and the route hands the request to the chosen implementation. In a class diagram for a refactor, draw each controller as a class and connect both to the interface they should satisfy. The calling relationship and the responsibilities of each database implementation are then visible.5

Controller class diagram. The Express POST /assets route depends on the AssetController interface. SQLite and SQL Server controllers each implement createAsset(req, res).

The class diagram abstracts AssetMan's common calling convention as AssetController for discussion during refactoring.5

If the only task is adding field validation during creation, giving the AI the route, controller method, and table definitions is enough. When replacing controllers or redistributing responsibilities, give it the class diagram and the interface contract as well.

How do you draw a composite primary key in a UML class diagram?

When upgrading memory in computer asset 101, look up the extra record with (101, '内存容量') (“memory capacity”) and update asset_field_value. In a UML class diagram, mark both asset_id and asset_field_name as {id}: together the properties identify an instance. Whether those properties are also the database primary key depends on the DDL. AssetMan explicitly declares PRIMARY KEY (asset_id, asset_field_name), so the diagram's right side also marks both columns with «PK», borrowed from Rational's data modeling profile.11012

GLPI's computer details page has separate fields for Serial number and Inventory number.13 When drawing an ER diagram for your own system, ask: within what scope is the inventory number unique? Can devices from different manufacturers have the same serial number?

Composite key notation. AssetExtra's asset ID and field name share one primary key; device type and field value are ordinary attributes. A hypothetical device register has two composite candidate keys: organization code and inventory number are both CK1, chosen as the primary key; manufacturer code and serial number are both CK2, with a unique constraint. A custom legend uses id1 and id2 for the two groups.

The upper half follows AssetMan's SQLite table definition. The lower half illustrates grouping. It assumes all four identifier fields are required, inventory numbers are unique within an organization, and serial numbers are unique within a manufacturer.10

In the lower half, either group of properties uniquely identifies a device, and removing one column from either group loses that property. Both are therefore composite candidate keys. Under the {CK1}, {CK2} grouping convention, put the same number on both rows belonging to a group. A table can select at most one group as its primary key. Here that's organization code and inventory number. A two-column UNIQUE constraint enforces uniqueness for manufacturer code and serial number.1415

Standard UML 2.5.1 uses unnumbered {id} to mark identifying properties. All properties marked this way form one identifier together. If you want to group properties with {id1} and {id2}, define those numbers in the legend. The custom legend in the lower half maps {id1} to the first candidate key and {id2} to the second.1

Give the AI executable acceptance criteria with the diagram

For AssetMan, I'd narrow the implementation requirements to one small diagram and a few testable statements. First ask the AI to identify where to make changes by comparing the existing route, the two tables being written, and the test entry point. Then specify the following outcomes and ask it to verify them using the repository's existing testing approach.

  • Submit a valid asset and two extra values. Return 201 and the new asset ID only when both kinds of records are written successfully.
  • Cause the second extra value write to fail. After the request fails, query the database with the same fixture and confirm that neither the base record nor any extra values from this creation remain.
  • Allow creation without extra values and return success after the base record commits.

At delivery, I can then check the implementation through both the API response and the database result. If the diagram itself is the interface in which users write programs, the design also needs to explain how users can predict behavior from a connection. I explore that in What does a line on a workflow canvas actually mean?

Footnotes

  1. Object Management Group. (2017, December). OMG Unified Modeling Language (OMG UML), Version 2.5.1. OMG. Specification PDF, §§9.5.3–9.5.4, Properties (pp. 113–114, multiproperty identifiers and id notation), §11, Structured Classifiers, and §17, Interactions. ↩ ↩2 ↩3

  2. Chen, P. P. (1976). The entity-relationship model—toward a unified view of data. ACM Transactions on Database Systems, 1(1), 9–36. MIT paper archive. ↩

  3. Visual Paradigm. (n.d.). What is Sequence Diagram? Visual Paradigm. Final paragraph of Time Dimension. Visited in Firefox on December 30, 2024. ↩

  4. Bell, D. (2023, October 8). Explore the UML sequence diagram. IBM Developer. Alternatives, opening paragraph, Loops, and Break. Visited in Firefox on December 30, 2024. ↩ ↩2 ↩3

  5. CaiBorui. (2025, June 29). AssetMan [Local Git snapshot 6ea5286]. The DB_TYPE configuration and POST /assets route in backend/app.js, auth and roleAuth in backend/middlwares/auth/index.js, and both database controllers' createAsset methods. ↩ ↩2 ↩3 ↩4

  6. SQLite. (2026, February 18). Transaction. SQLite Documentation. Transactions and Response To Errors Within A Transaction. ↩

  7. Grokability. (n.d.). Snipe-IT [Source repository]. GitHub. Project repository. ↩

  8. DrawSQL. (n.d.). Snipe-IT database schema [Database relationship diagram]. DrawSQL. Snipe-IT table structure. ↩

  9. CaiBorui. (2025, April 17). AssetMan data design [Draw.io diagrams retained by the author]. Date from the local file timestamp. The illustration is taken from Untitled Diagram(6).drawio, retaining the Asset_Base, Asset_Info, and Asset_Type_Template boxes and their annotations. asset_man_class.drawio is another version from the same day. ↩ ↩2

  10. CaiBorui. (2025, April 17). Add primitive backend service [AssetMan local Git commit 21f7ffe]. SQLite table definitions for asset_base, asset_extra, and asset_type_template in backend/app.js. Later snapshot 6ea5286 places them in backend/databases/init.js. ↩ ↩2 ↩3

  11. SQLite. (n.d.). SQLite Foreign Key Support. SQLite Documentation. Introduction to Foreign Key Constraints. ↩

  12. Gornik, D. (2003). UML Data Modeling Profile. Rational Software. “Primary Key” section. ↩

  13. glpi-project. (n.d.). asset.png [Computer details page screenshot]. GitHub. Serial number and Inventory number in the Computer form's right column. ↩

  14. Blaha, M., & Premerlani, W. (1998). Object-oriented modeling and design for database applications. Prentice Hall. §3.1.4, “Candidate Keys for Classes,” figure 3.4 and first paragraph after figure. ↩

  15. SQLite. (2025, April 30). CREATE TABLE. SQLite Documentation. §3.5, The PRIMARY KEY and §3.6, UNIQUE constraints. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-04-17). Before asking AI to implement a feature, what should you put in a sequence diagram, an ER diagram, or a class diagram?. https://caiborui.com/blog/2025-04-17-zh-design-diagrams-for-ai-implementation-entrans

@misc{borui2025-febd5223,
  author = {borui},
  title = {{Before asking AI to implement a feature, what should you put in a sequence diagram, an ER diagram, or a class diagram?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-04-17-zh-design-diagrams-for-ai-implementation-entrans}
}