@@ -438,3 +438,72 @@ def test_validation_result_summary_truncated(self, schema_insert):
438438 result = dj .ValidationResult (is_valid = False , errors = errors , rows_checked = 20 )
439439 summary = result .summary ()
440440 assert "and 10 more errors" in summary
441+
442+
443+ class AllDefaultsTable (dj .Manual ):
444+ """Table where all attributes have defaults."""
445+
446+ definition = """
447+ id : int auto_increment
448+ ---
449+ timestamp=CURRENT_TIMESTAMP : datetime
450+ notes=null : varchar(200)
451+ """
452+
453+
454+ class TestEmptyInsert :
455+ """Tests for inserting empty dicts (GitHub issue #1280)."""
456+
457+ @pytest .fixture
458+ def schema_empty_insert (self , connection_test , prefix ):
459+ schema = dj .Schema (
460+ prefix + "_empty_insert_test" ,
461+ context = dict (AllDefaultsTable = AllDefaultsTable , SimpleTable = SimpleTable ),
462+ connection = connection_test ,
463+ )
464+ schema (AllDefaultsTable )
465+ schema (SimpleTable )
466+ yield schema
467+ schema .drop ()
468+
469+ def test_empty_insert_all_defaults (self , schema_empty_insert ):
470+ """Test that empty insert succeeds when all attributes have defaults."""
471+ table = AllDefaultsTable ()
472+ assert len (table ) == 0
473+
474+ # Insert empty dict - should use all defaults
475+ table .insert1 ({})
476+ assert len (table ) == 1
477+
478+ # Check that values were populated with defaults
479+ row = table .fetch1 ()
480+ assert row ["id" ] == 1 # auto_increment starts at 1
481+ assert row ["timestamp" ] is not None # CURRENT_TIMESTAMP
482+ assert row ["notes" ] is None # nullable defaults to NULL
483+
484+ def test_empty_insert_multiple (self , schema_empty_insert ):
485+ """Test inserting multiple empty dicts."""
486+ table = AllDefaultsTable ()
487+
488+ # Insert multiple empty dicts
489+ table .insert ([{}, {}, {}])
490+ assert len (table ) == 3
491+
492+ # Each should have unique auto_increment id
493+ ids = set (table .to_arrays ("id" ))
494+ assert ids == {1 , 2 , 3 }
495+
496+ def test_empty_insert_required_fields_error (self , schema_empty_insert ):
497+ """Test that empty insert raises clear error when fields are required."""
498+ table = SimpleTable ()
499+
500+ # SimpleTable has required fields (id, value)
501+ with pytest .raises (dj .DataJointError ) as exc_info :
502+ table .insert1 ({})
503+
504+ error_msg = str (exc_info .value )
505+ assert "Cannot insert empty row" in error_msg
506+ assert "require values" in error_msg
507+ # Should list the required attributes
508+ assert "id" in error_msg
509+ assert "value" in error_msg
0 commit comments