Quote any references to tag.id in raw sql

Rebase against master of pull request #399.
Credits go to @awt .