Database error when adding short URLs

mkalkbrenner - September 15, 2009 - 11:03
Project:Knurl
Version:6.x-1.1
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:needs review
Description

Using version 6.x-1.1 of knurl it's impossible to add more than one short url!

The reason is, that you're using a field called 'tid' as primary key which is neither incremented in your code nor an auto increment field in the database. (Might be some old code).

Unfortunetely you don't handle database errors and report success to the user. But the database error could be found in watchdog:
Duplicate entry for key 1 query: INSERT INTO knurl (uid, short_url, link) values ...

Attached you'll find a patch that changes the database schema using hook_update for a 6.x-1.2 release. Additionally I changed the code for saving a new short URL to deal with database errors.

AttachmentSize
fix_db_schema.patch3.99 KB

#1

schandur - September 15, 2009 - 15:07

Hi Markus,

Any reason why you decided to drop the tid field? In my local setup, I changed the 'tid' field's type to 'serial' (in hook_schema) and it seems to be working fine.

Cheers,
Sandeep

#2

mkalkbrenner - September 16, 2009 - 09:31

Hi schandur,

I searched the code for tid and didn't find any real need for it.
On the other hand most queries use an index over short_url to get their result.
So the benefit of droping tid and declaring short_url as primary key is that you have one index less on your database (which only matters in large scale environments in different ways) and that short_url becomes unique on the database layer (which could also be achieved via an additional unique constraint in schema).
Additionally tid isn't a good name for that field because most people would associate taxonomy term ids with tid.

Markus

 
 

Drupal is a registered trademark of Dries Buytaert.