[Qgis-developer] Pyqgis add field to PostGIS table fails

Spencer Gardner spencergardner at gmail.com
Wed Oct 28 19:46:27 PDT 2015


I'm trying to add a field to my PostGIS table using the following code:

--------------------------------------------------------
from PyQt4.QtCore import QVariant
from qgis.core import *

inLayer = iface.activeLayer()
provider = inLayer.dataProvider()

# add field
field = QgsField('test_field', QVariant.String)
inLayer.startEditing()
provider.addAttributes([field])
inLayer.updateFields()

# commit
inLayer.commitChanges()
--------------------------------------------------------

The commit fails and the PostGIS log shows an error:

2015-10-27T08:13:28 1 Erroneous query: ALTER TABLE "received"."env" ADD
COLUMN "test_field" returned 7 [ERROR: syntax error at end of input

LINE 1: ALTER TABLE "received"."env" ADD COLUMN "test_field"

Seems the call to PostGIS fails to include the column type. Is this a bug,
or am I going about this the wrong way?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20151028/e2cdb7ed/attachment.html>


More information about the Qgis-developer mailing list