@@ -83,7 +83,7 @@ def add():
8383 "notes" : form .get ("notes" ),
8484 }
8585 # Prevent duplicate (series, unique_id) combination
86- from sqlalchemy import and_
86+ from sqlalchemy import and_ , func
8787
8888 from ..extensions import session_scope
8989 from ..models .miniature import Miniature
@@ -95,8 +95,27 @@ def add():
9595 .first ()
9696 )
9797 if existing :
98- flash (f"Unique ID { unique_id } already exists in Series { series } " , "danger" )
99- return render_template ("miniatures/add.html" )
98+ # Calculate next available unique_id in this series
99+ max_unique = (
100+ session .query (func .max (Miniature .unique_id ))
101+ .filter (Miniature .series == series )
102+ .scalar ()
103+ ) or 0
104+ next_unique = max_unique + 1
105+
106+ # Preserve all form fields and suggest next ID
107+ prefill = {
108+ "series" : series ,
109+ "unique_id" : next_unique ,
110+ "prefix" : form .get ("prefix" ),
111+ "chassis" : form .get ("chassis" ),
112+ "type" : form .get ("type" ),
113+ "status" : form .get ("status" ),
114+ "tray_id" : form .get ("tray_id" ),
115+ "notes" : form .get ("notes" ),
116+ }
117+ flash (f"Unique ID { unique_id } already exists in Series { series } . Suggested next ID: { next_unique } " , "danger" )
118+ return render_template ("miniatures/add.html" , prefill = prefill )
100119
101120 add_miniature (data )
102121 flash ("Miniature added" , "success" )
0 commit comments