There are two bugs in this issue:
1) The deletedPkQuery must say "select field from table". It may not say "select expression" or "select field AS FIELD from table". The former does not work and the latter may work with some databases and not others. In other words, the first example works and the others fail:
deletedPkQuery="select id from item where EXPRESSION"
deletedPkQuery="select concat('',id) from item where EXPRESSION"
deletedPkQuery="select id from item AS ID where EXPRESSION"
deletedPkQuery="select id from item AS ITEM.ID where EXPRESSION"
2) If a template is used to format the contents of the Solr primary key, that template is declared at the <field> level and is not visible in the parent <entity> level. Since the deletedPkQuery is declared in the <entity> level, it cannot see the field's template. Thus the template cannot be applied to the results of the deletedPkQuery. Any formatting in the <field> template must be duplicated (in SQL syntax) in the deletedPkQuery.
This is workaround that achieves the goal of #2: if the Solr primary key is declared as
<field name="id" template="prefix-$
then the deletedPkQuery must be declared as
deletedPkQuery="select concat('prefix',id) from item where EXPRESSION"
Because of bug #1, it is not possible to use this workaround.
The attached patch fixes bug #1, making it possible to use the workaround. It is Erik's second patch but only with the fix for bug #1. It does not create an automatic way for results of deletedPkQuery to be formatted with the primary key's template.