@@ -440,3 +440,42 @@ class Test(ModelBase, table_name="table", primary_key="id"):
440440 assert '"name"=EXCLUDED."name"' not in partial_query # Excluded manually
441441 assert '"updated_at"=EXCLUDED."updated_at"' in partial_query
442442 assert '"created_at"=EXCLUDED."created_at"' in partial_query # Force updated
443+
444+
445+ def test_primary_key_only_table_upsert ():
446+ """Test that upsert works correctly for tables with only primary key columns."""
447+
448+ @dataclass
449+ class PrimaryKeyOnly (ModelBase , table_name = "pk_only" , primary_key = "id" ):
450+ id : uuid .UUID
451+
452+ test_instance = PrimaryKeyOnly (uuid .uuid4 ())
453+ insert_sql = test_instance .insert_sql ()
454+
455+ # Test that upsert generates valid SQL with DO NOTHING
456+ upsert_sql = PrimaryKeyOnly .upsert_sql (insert_sql )
457+ query , params = upsert_sql .query ()
458+
459+ # Should contain ON CONFLICT DO NOTHING since there are no updatable fields
460+ assert "ON CONFLICT" in query
461+ assert "DO NOTHING" in query
462+ assert "DO UPDATE SET" not in query
463+ assert len (params ) == 1 # Only the ID parameter
464+
465+ # Test with compound primary key (still no other fields)
466+ @dataclass
467+ class CompoundPrimaryKeyOnly (
468+ ModelBase , table_name = "compound_pk_only" , primary_key = ("id1" , "id2" )
469+ ):
470+ id1 : int
471+ id2 : str
472+
473+ compound_instance = CompoundPrimaryKeyOnly (1 , "test" )
474+ compound_insert = compound_instance .insert_sql ()
475+ compound_upsert = CompoundPrimaryKeyOnly .upsert_sql (compound_insert )
476+ compound_query , compound_params = compound_upsert .query ()
477+
478+ assert "ON CONFLICT" in compound_query
479+ assert "DO NOTHING" in compound_query
480+ assert "DO UPDATE SET" not in compound_query
481+ assert len (compound_params ) == 2 # Both ID parameters
0 commit comments