The template works for me, so unfortunately it isn't obvious why it doesn't work for you.
Questions:
- How many virtual libraries do you have?
- What version of calibre are you running?
- Do any other columns built from other columns reference #collection
The easiest way to debug this is probably for you to send me a copy of your metadata.db, preferably one that contains the error. I need only the db file, no books. Send it to calibre-bugs at charles dot haleys dot org. If it is large, more than a megabyte or so, please zip it first.