Skip to article frontmatterSkip to article content

Insert

(This is an AI-generated placeholder -- to be updated soon.)

DataJoint provides two primary commands for adding data to tables: insert and insert1. Both commands are essential for populating tables while ensuring data integrity, but they are suited for different scenarios depending on the quantity and structure of the data being inserted.

Overview of insert1

The insert1 command is used for adding a single row of data to a table. It expects a dictionary where each key corresponds to a table attribute and the associated value represents the data to be inserted.

Syntax

<Table>.insert1(data, ignore_extra_fields=False)

Parameters

  1. data: A dictionary representing a single row of data, with keys matching the table’s attributes.
  2. ignore_extra_fields (default: False):
    • If True, attributes in the dictionary that are not part of the table schema are ignored.
    • If False, the presence of extra fields will result in an error.

Example

import datajoint as dj

schema = dj.Schema('example_schema')

@schema
class Animal(dj.Manual):
    definition = """
    animal_id: int  # Unique identifier for the animal
    ---
    species: varchar(64)  # Species of the animal
    age: int             # Age of the animal in years
    """

# Insert a single row into the Animal table
Animal.insert1({
    'animal_id': 1,
    'species': 'Dog',
    'age': 5
})

Key Points

  • insert1 is ideal for inserting a single, well-defined record.
  • It ensures clarity when adding individual entries, reducing ambiguity in debugging.

Overview of insert

The insert command is designed for batch insertion, allowing multiple rows to be added in a single operation. It accepts a list of dictionaries, where each dictionary represents a single row of data.

Syntax

<Table>.insert(data, ignore_extra_fields=False, skip_duplicates=False)

Parameters

  1. data: A list of dictionaries, where each dictionary corresponds to a row of data to insert.
  2. ignore_extra_fields (default: False):
    • If True, any extra keys in the dictionaries are ignored.
    • If False, extra keys result in an error.
  3. skip_duplicates (default: False):
    • If True, rows with duplicate primary keys are skipped.
    • If False, duplicate rows trigger an error.

Example

# Insert multiple rows into the Animal table
Animal.insert([
    {'animal_id': 2, 'species': 'Cat', 'age': 3},
    {'animal_id': 3, 'species': 'Rabbit', 'age': 2}
])

Key Points

  • insert is efficient for adding multiple records in a single operation.
  • Use skip_duplicates=True to gracefully handle re-insertions of existing data.

Best Practices

  1. Use insert1 for Single Rows: Prefer insert1 when working with individual entries to maintain clarity.
  2. Validate Data Consistency: Ensure the input data adheres to the schema definition.
  3. Batch Insert for Performance: Use insert for larger datasets to minimize database interactions.
  4. Handle Extra Fields Carefully: Use ignore_extra_fields=False to detect unexpected keys.
  5. Avoid Duplicates: Use skip_duplicates=True when re-inserting known data to avoid errors.

Summary

  • Use insert1 for single-row insertions and insert for batch operations.
  • Both commands enforce schema constraints and maintain the integrity of the database.
  • Proper use of these commands ensures efficient, accurate, and scalable data entry into your DataJoint pi