sql server - how to make a field auto increment in a table in grails? and when deleted it adjust the other records -


i have table called trip users can create trips, , on edit screen have button users can click on , create legs selected trip. question how make field in triplegs domain auto increment? lets user creates 4 trip legs, stop number field "the 1 wanted auto populate" 1 2 3 4 if user goes , delete trip leg 2 how change stop number in rest 3 legs 1 2 3 instead of 1 3 4 enter image description here

i have agree vahid's comment on original question. preferred approach dynamically set value transient value on domain after sorted collection criteria.

if wanted maintain same sort order every time based on leg created sequentially, add 'created_date' column table , sort based on value when list.

alternatively, if store stop_number value (which there plenty of reasons why might want to) might want consider following approach:

override default delete action triplegs:

class triplegcontroller {   def delete = {     def triplegid = params.triplegid long     def tripleg = tripleg.get(triplegid)     def otherlegs = tripleg.findallbytripandidnotequal(tripleg.trip, triplegid, [sort: 'stopnumber', order: 'asc'])     tripleg.delete(failonerror: true)     def stopnum = 1     otherlegs.each{leg ->        leg.stopnumber = stopnum       leg.save()       stopnum++     }   } } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -