Fixed more issues, but now the big one is the INSERT ... WHERE NOT EXISTS syntax.  That needs to have a real table to select against, and we need to return exactly 1 row (limit 1 helps if there are more than 1).  Need to find the right table to use, or an alternative syntax.  Worst case scenario is writing a trigger.
Backed out the fancy insertion for the session id - it did not actually work.  I just switched it over to a unique index on a varchar(700), as that is within the constraints, and the value is base64 encoded, so we do not need to worry about out-of-range characters.