G-XR8P2XJ088

AI-Written SQLite: Check That the Relationship Is Enforced

A tutor asks an AI coding assistant to build a small booking database. Every booking should belong to a customer. The generated table definition includes a reference to the customer table, and the tutor can create and list bookings. Everything appears to work. Then an import adds a booking with a customer ID that does not exist. It saves too. The relationship is written in the database design, but the connection doing the import has not enabled its enforcement.

This hypothetical mistake is easy to miss when you test only valid records. A booking linked to a real customer succeeds whether the relationship is being checked or not. To find out whether the database protects the rule, you need a test that deliberately breaks it. That is a useful habit for anyone reviewing AI-written code: ask what should be refused, then try that case in a disposable test database.

A declaration and a running check are different things

SQLite’s official foreign key documentation explains that applications should explicitly configure enforcement for each connection rather than assume a default. The command PRAGMA foreign_keys = ON enables it on a supporting build, and querying PRAGMA foreign_keys checks the setting. It also warns that changing the setting during an active multi-statement transaction has no effect. Put connection setup before the work begins, then verify the setting on the connection that will actually write the records.

In the tutor’s case, there may be more than one route into the database. The booking screen opens a connection. A nightly import opens another. A maintenance script may open a third. Seeing enforcement enabled in the app proves little about the script unless both use the same verified setup. Ask the assistant to show where each writer gets its connection and how that path is tested.

Make the rule concrete

Write down what an accepted booking looks like. For example: a customer exists, a booking contains that customer’s ID, and the ID is required. A nullable reference is a different rule: SQLite permits a null child key without a corresponding parent. If every booking must have a customer, the design needs to express that requirement too. Do not substitute a made-up ID or an empty name merely to get an import through.

Now choose the intended behaviour when someone deletes a customer who still has bookings. Perhaps the business keeps the customer record while related bookings exist. Perhaps it has another carefully designed removal process. The table definition should reflect the chosen behaviour, and the test should demonstrate it. Avoid accepting a cascade deletion simply because generated code includes it. In a scheduling tool, removing historical bookings may be far more than the person clicking ‘delete customer’ meant to do.

Test both the allowed and the forbidden case

Create a disposable database using the same setup route as the application. Add a customer, then add a booking linked to that customer and confirm it saves. Next, try a booking linked to an ID that is absent. Check for an explicit failure and confirm that the invalid booking was not stored. If the script reports the failure but carries on, inspect the final database as well as the message. A reassuring log line is not the record itself.

  • Repeat the invalid-reference test through the importer, not just through the booking screen. That catches a writer with different connection settings.
  • Try a missing customer ID if the rule requires one. Check that the failure tells the operator which row needs attention without exposing unrelated customer details.
  • Test the chosen deletion behaviour with a customer who has a booking. Check both tables afterwards, including the records you intended to preserve.

Our article on customer totals in AI-written Python illustrates another benefit of a deliberately awkward example. Clean sample data can make two different behaviours look the same. Here, the valid booking is the clean sample. The absent customer is the example that reveals whether the promised protection is running.

Review the data already stored

If you discover that one writer has been operating without enforcement, decide what to do about existing records before announcing a fix. Enabling the setting for future writes is only part of the repair. Reconcile the stored bookings against the customer records in a copy of the database, investigate unmatched IDs and keep a record of the decisions. A booking may need a corrected ID, a restored customer record or a manual review. Inventing a generic customer to absorb every mismatch can hide the import error rather than resolve it.

Keep that review separate from changing production data. Once the correction plan is clear, back up the database and test the repair against the copy before applying it. The owner should be able to explain which records changed and why. Afterward, rerun the forbidden-case tests through each writer so the next import cannot quietly recreate the problem.

The question to ask an AI assistant is simple: ‘Show me that this connection rejects a booking for a customer who does not exist.’ That turns a line in a schema into a behaviour you can observe. It gives the next person maintaining the tool a clear rule to preserve.

Source


Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe to get the latest posts sent to your email.

Leave a Reply

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe now to keep reading and get access to the full archive.

Continue reading