Skip to content

Writing to Entry Tables from make()

A make() normally writes one thing: its own row, and the rows of its Part tables. The dependency graph then records everything about where that row came from. The row is a foreign-key child of the rows it was computed from, so it cannot exist without them, and deleting them deletes it.

Some computations also have to create rows that are not their own result. They create rows that the pipeline will treat as new inputs. A make() may add such rows to entry tables (dj.Manual), the tables where data enters the pipeline, provided that the origin of each row it writes is recorded. This page explains why that is sometimes the right design, what it gives up, and what it requires in return. The recipes are in Write Entry Rows from make().

What the dependency graph cannot record

A row written into an entry table is not a foreign-key child of the job that wrote it. dj.Diagram shows no edge from the job to the table, deleting the job's row does not delete the rows it wrote, and no query can follow the link. The graph has nothing to say about where these rows came from. That is what entry tables are for: they are the pipeline's boundary, where rows arrive from outside its dependency structure.

So the origin has to be recorded in two other ways, which do different jobs:

  • DataJoint records it automatically. With origin capture on, every row inserted into an entry table carries a hidden _prov attribute, and a row inserted from inside a make() records the table and key of the job that wrote it. This holds for rows inserted as values (dicts, tuples, or a DataFrame). Rows copied by inserting a query keep the _prov of their source rows, or none, and do not record the job. No code in the make() asks for this, and no insert can set or forge it. See Extrinsic Provenance at Entry Tables.
  • You model the link that the pipeline itself needs. Hidden attributes cannot be joined or restricted the way ordinary attributes can. When downstream code has to follow a row back to what produced it, keep that link in an ordinary attribute: the source_file of a parsed record, or the iteration of an iterative computation. _prov is the audit record; the modeled attribute is the domain link.

When a foreign key is wrong or impossible

Prefer a foreign key whenever one can express the relationship. A result that is derived from its inputs belongs in a Computed or Imported table that depends on them. Write into an entry table only when a foreign key from the new rows to the job would be wrong (it would distort the domain model) or impossible (it would close a cycle). Some common cases:

  • Fan-out ingestion. One ingestion step reads one source and creates rows in several independent tables: one acquisition file yields Subject, Session, and Recording records. Making those tables children of the loader would tie the stable domain model to changeable infrastructure: every change to how data is loaded would become a schema migration. See Fan-out ingestion.
  • Iterative computations. A computation refines an estimate step by step, and each step's output is the next step's input. The table graph must not have cycles, so the next iterate cannot be a child of the step that produced it. Writing it into the entry table that the step reads from closes the loop through the data instead. Each populate() advances the computation by one step, and the make() stops writing when the estimate has converged.

    An iterative computation. Problem, a green rounded box for the Manual tier, is joined by a thin navy line to Estimate, another Manual box, whose name is underlined because it adds the iteration attribute to the key. Estimate is joined by a thick navy line to Refine, an orange ellipse for the Computed tier, whose key is exactly Estimate's. A dashed navy arrow runs from Refine back to Estimate, labelled "insert iteration k + 1, until converged". A note reads "no foreign key from Estimate to Refine: it would close a cycle". A legend keys the Computed and Manual tiers, distinguishes a dependency (no arrowhead) from a write that is not a dependency, and notes that an underlined name introduces a primary-key attribute of its own.An iterative computation. Problem, a green rounded box for the Manual tier, is joined by a thin navy line to Estimate, another Manual box, whose name is underlined because it adds the iteration attribute to the key. Estimate is joined by a thick navy line to Refine, an orange ellipse for the Computed tier, whose key is exactly Estimate's. A dashed navy arrow runs from Refine back to Estimate, labelled "insert iteration k + 1, until converged". A note reads "no foreign key from Estimate to Refine: it would close a cycle". A legend keys the Computed and Manual tiers, distinguishes a dependency (no arrowhead) from a write that is not a dependency, and notes that an underlined name introduces a primary-key attribute of its own.

    Optimization rounds, model retraining on accumulating labels, and curation passes that propose the next round of work have the same shape. See Iterative computations. - Closed-loop and adaptive experiments. An analysis of the last session chooses the stimulus, condition, or dose for the next one and registers it as a planned row. The planned row is an input to the next experiment, not a result of the analysis. - A selected result becomes a parameter. An optimization chooses the best parameter set, and the pipeline then computes with it as with any other. Write it as a new row of a parameter table that is an entry table. A Lookup table does not fit: its rows come from its declared contents, and it records no origin. - Work queues for people. A quality check finds items that need a person's review and adds them to a review table, where the reviewer's decisions are entered.

What a make() must do when it writes entry rows

The write is part of the job, and these rules keep it so:

  • Write in the same transaction as the job's own row. A make() runs in one transaction, so if it fails, the entry rows it wrote are removed with everything else. In a tripartite make() the writes belong in make_insert.
  • Derive the written keys from the job's key. Iteration k writes iteration k + 1; file 7 writes the subject parsed from file 7. A re-run of the same job then targets the same rows, and two different jobs never target the same row.
  • Add rows; never change rows that results were computed from. Modifying an existing entry row would change the inputs of results that are already stored.
  • Insert values, not a query. Rows copied from a query do not record the job.
  • Write only to entry tables. Computed and Imported tables accept inserts only from their own make(). Lookup tables hold their declared contents.
  • Turn on origin capture for the deployment, so that _prov exists on the target tables. Tables declared while capture was off need the column added; see Record Data Origin.

When the row already exists

The rows a job writes outlive the job's own row. Deleting a Refine row does not delete the Estimate row it wrote, because no foreign key connects them. So the row a make() is about to insert may already be there:

  • the job's own row was deleted and the job runs again;
  • the row was entered directly, by a person or another loader;
  • two jobs target the same row, because the written keys were not derived from the job keys.

The make() has to decide what to do, and there are four choices:

Choice What happens Use it when
Error (plain insert1) The insert raises a duplicate-key error, the whole make() is rolled back, and populate() raises the error (with reserve_jobs=True, the job is also marked as an error in the jobs table). You expect no existing row: one would point to a modeling mistake. This is the safe default.
Verify (in your code) Compare the stored row with the new one; insert has no flag for this, so the make() does it explicitly. Skip the insert if they are equal, raise an error if they differ. Jobs may run again. A re-run that reproduces the same row succeeds, and one that would produce a different row stops.
Skip (skip_duplicates=True) The existing row is kept, with its values and its _prov, even if the new values differ. Nothing reports the difference. The new row is certain to equal the stored one, so comparing is not needed.
Replace (replace=True) The existing row is deleted and the new one inserted, with a new _prov. It is refused if any other row depends on the existing row. The stored row is known to be wrong, and nothing has been computed from it.

Verify is usually the right choice for writes that a re-run can repeat. Skipping looks equivalent, but it hides the case that matters: a re-run that would have produced different values. Replacing discards the record of where the old row came from. When rows have already been computed from it, replacing is refused, and the correction is a deliberate delete() followed by a new run, not something a make() should decide on its own.

replace=True on PostgreSQL

On PostgreSQL, insert(..., replace=True) currently fails with a syntax error (datajoint-python#1780). Delete the stored row and insert the new one instead.

See also