Bug report #21615

refactorfields algorithm fails for field names that start with a digit

Added by Peter Gipper almost 5 years ago. Updated almost 5 years ago.

Affected QGIS version:3.4.5 Regression?:No
Operating System:Windows 7 Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:29431


If a vector layer has field names starting with digits (i.e. "1_field") the algorithm 'refactorfields' throws an exception:
syntax error, unexpected NAME, expecting $end

Not sure if this is a bug or just not supported. When running refactorfields via QGIS GUI there are no problems.

To reproduce, execute the following code in qgis python console:

layer = QgsVectorLayer('Point?crs=epsg:4326&field=1_field:string(10)', 'test', 'memory')
field = layer.fields()[0]
field_map = [{
'name': field.name(),
'type': field.type(),
'length': field.length(),
'precision': field.precision(),
'expression': field.name()
'INPUT': layer,
'FIELDS_MAPPING': field_map,


#1 Updated by Giovanni Manghi almost 5 years ago

  • Category changed from Processing/Core to Processing/QGIS

#2 Updated by Nyall Dawson almost 5 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

The expression needs to be


i.e. the field name MUST be double quoted. When running through the GUI it's automatically done (unless you manually edit the expression). You need to update your code to:

field_map = [{
'name': field.name(),
'type': field.type(),
'length': field.length(),
'precision': field.precision(),
'expression': '\"field.name()\"'

Also available in: Atom PDF