SQL insert into a many-to-many link table fails with a referential integrity constraint error: the constraint lives in the database

Applies to: a Clarion application (the thread says CW and the templates; no version named) against a SQL database, as reported in 2007.

Problem

With two tables (PROGRAMS and COURSES) and a link table between them, each with a unique primary key and the relationships set up in the dictionary, inserting a record into the link table failed. The error complained about a referential integrity constraint on the CourseID field. Removing the autonumber did not change it.

Solution

  1. Understand the message: the foreign key constraint is set in the SQL database, not in the dictionary or the application. It refuses a child row until the parent row exists, and the program cannot override it.
  2. Insert the records in the order the database requires, parent first. If the database autonumbers the parent, get the number from the database before writing the children.
  3. In this case the poster found the dictionary was the cause: he removed the relationships and keys in question, restarted the PC and Clarion, re-created the keys and relationships the same way, and the insert worked without touching the application.
  4. For a screen that edits a parent and a child on one form (two browses, two sets of update buttons) a poster advised doing part by hand: write the parent first. Get the key value from a stored procedure (Firebird, Ingres) or, on MS SQL, from the new autonumbering that puts the value in the field after the insert.

Caveats

  • The thread does not say why the re-created keys behaved differently; the poster wrote "I hope I fixed it".
  • The same-form parent and child case was described by the poster and answered in general terms, not tested in the thread.

Today (2026)

Likely unchanged for the database side: a foreign key constraint is enforced by the server whatever the client does. Not re-tested.

Sources: Arthur K. (Jul 2007, the question, and Aug 2007), Ron J., John G., Sean H. (Aug 2007) in softvelocity.clarion.databasedrivers.