Bug report #21615
refactorfields algorithm fails for field names that start with a digit
Status: | Closed | ||
---|---|---|---|
Priority: | Normal | ||
Assignee: | - | ||
Category: | Processing/QGIS | ||
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 |
Description
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()
}]
processing.runAndLoadResults('qgis:refactorfields',{
'INPUT': layer,
'FIELDS_MAPPING': field_map,
'OUTPUT':'memory:test_refactored'
})
History
#1 Updated by Giovanni Manghi over 5 years ago
- Category changed from Processing/Core to Processing/QGIS
#2 Updated by Nyall Dawson over 5 years ago
- Resolution set to invalid
- Status changed from Open to Closed
The expression needs to be
"1_field"
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()\"'
}]