duplicate key value violates unique constraint "ticket_pkey"

How to Fix the Error with Duplicate Key Value Violates Unique Constraint "ticket_pkey"
 
After trying to migrate data over from a database, sometimes the following error results when trying to start TACTIC:

ERROR:
duplicate key value violates unique constraint "ticket_pkey"
 
Below is a tutorial on how to fix that error on Postgres.
 
 
--
 
Below is the error when trying to start TACTIC:
 
-bash-4.1$ python /opt/southpaw/tactic/src/bin/startup_dev.py 
 
Data Directory [/opt/southpaw/tactic_data]
Asset Directory [/opt/southpaw/assets]
Temp Directory [/opt/southpaw/tactic_temp]
Config path [/opt/southpaw/tactic_data/config/tactic-conf.xml]
Error with query (Error):  sthpw INSERT INTO "ticket" ("timestamp", "ticket", "login", "expiry") VALUES (now(), '1489f340e845c3c54bf6ae2e99d25164', 'admin', now() + '10 hour'::interval)
Traceback (most recent call last):
  File "/opt/southpaw/tactic/src/bin/startup_dev.py", line 154, in <module>
    startup(port, options.server)
  File "/opt/southpaw/tactic/src/bin/startup_dev.py", line 71, in startup
    startup = CherryPyStartup(port)
  File "/opt/southpaw/tactic/src/pyasm/web/cherrypy_startup.py", line 191, in __init__
    Batch()
  File "/opt/southpaw/tactic/src/pyasm/security/batch.py", line 46, in __init__
    my._do_login()
  File "/opt/southpaw/tactic/src/pyasm/security/batch.py", line 64, in _do_login
    security.login_as_batch(my.login_code)
  File "/opt/southpaw/tactic/src/pyasm/security/security.py", line 897, in login_as_batch
    my._ticket = my._generate_ticket(login_name)
  File "/opt/southpaw/tactic/src/pyasm/security/security.py", line 1198, in _generate_ticket
    ticket = Ticket.create(ticket_key,login_name, expiry, category=category)
  File "/opt/southpaw/tactic/src/pyasm/security/security.py", line 698, in create
    ticket.commit(triggers=False)
  File "/opt/southpaw/tactic/src/pyasm/search/search.py", line 3001, in commit
    sql.do_update(statement)
  File "/opt/southpaw/tactic/src/pyasm/search/sql.py", line 748, in do_update
    raise SqlException(e.__str__())
pyasm.search.sql.SqlException: duplicate key value violates unique constraint "ticket_pkey"
 
 
 
SOLUTION:
From looking at the error in this stack trace, the next id for the login table needs to be set correctly.
Below is how to do fix this in Postgres:
 
# First, login into psql and determine the column that is affected
# by the constraint "ticket_pkey"
 
-bash-4.0$ psql -U postgres sthpw
psql (8.4.5)
Type "help" for help.
 
sthpw=# \d ticket
                                     Table "public.ticket"
  Column   |            Type             |                      Modifiers
 
-----------+-----------------------------+---------------------------------------------
--------
 id        | integer                     | not null default nextval('ticket_id_seq'::re
gclass)
 ticket    | character varying(100)      | not null
 login     | character varying(100)      |
 timestamp | timestamp without time zone | default now()
 expiry    | timestamp without time zone |
 category  | character varying(256)      |
 code      | character varying(256)      |
Indexes:
    "ticket_pkey" PRIMARY KEY, btree (id)
    "ticket_code_idx" UNIQUE, btree (code)
    "ticket_id_idx" UNIQUE, btree (id)
    "ticket_unique" UNIQUE, btree (ticket)
 
sthpw=#
 
 
# In this example, the constraint "ticket_pkey" affects the column named "id"
# The "id" column gets the next value from the sequence named "ticket_id_seq"
 
 
# Next, get the maximum value for id for the ticket table.
 
sthpw=# select max(id) from ticket;
   max
---------
 3066592
(1 row)
 
sthpw=#
 
 
# Then, get the current id from "ticket_id_seq"
 
sthpw=# select last_value from ticket_id_seq;
 last_value
------------
    2012345
(1 row)
 
sthpw=#
 
 
# Notice that 2012345  would be the next id, which is lower than 3066592.
# Finally, set the next value correctly for the sequence
 
select setval('ticket_id_seq', 3066593);
 
 
Now, try starting up TACTIC again.
 
System-Admin