1

Somehow I can't manage to add a feature to my postgres layer. Is this only possible in edit mode?

My layer

>>> cp 
<qgis._core.QgsVectorLayer object at 0x7f541c0e0348>

>>> cp.source()
u'dbname=\'work_viewtest\' host=myhost port=5432 user=\'b\' password=\'bxxxxxx\' key=\'id\' table="public"."v2_calculation_point" (the_geom) sql='

This is how I create my feature

f = QgsFeature()
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 456)))
f.setAttributes([10, 100, "ref", -1])
cp.dataProvider().addFeatures([f])
(True, [<qgis._core.QgsFeature object at 0x7f539ffa33e0>])
cp.updateExtents()
cp.updateFields()

The addFeatures() method returns True but I don't see any features in my layer, nor in the database table itself.

  • 1
    Haven't used postgres layers (yet) but my guess would be you need to edit the layer (cp.startEditing()) before you commit any changes (cp.commitChanges()). – Joseph Oct 14 '16 at 9:58
  • just tried that, returns True too but still no data in my layer.... – LarsVegas Oct 14 '16 at 10:05
  • 1
    @Joseph I found out what goes wrong: my postgres table as a foreign key constraint and the ID I'm passing to the data provider by thesetAttributes() does not exist in the reference relation. Still strange that the addFeatures() method returns True. – LarsVegas Oct 14 '16 at 10:48
  • Awesome, glad you found the problem! You should post it as an answer as I haven't seen another question like yours. I guess looking at the addFeatures(), it is mentioned that "Insert a copy of the given features into the layer (but does not commit it) ". As it is a boolean, it probably returns True because it has successfully copied the feature regardless if it has been committed. – Joseph Oct 14 '16 at 10:53
1

In case anyone stumbles across a similar problem. It seems as if the data provider does not check for insert errors in the database itself or least not for certain error types like foreign key constraints.

When I try to insert the data provided in my question by SQL:

INSERT INTO v2_calculation_point (id, content_type_id
  , user_ref
  , calc_type
  , the_geom) 
VALUES 
  (DEFAULT, 10, 'ref', -1, ST_GeomFromText('POINT(123 456)',28992))
;

I get the following error

ERROR: insert or update on table "v2_calculation_point" violates 
foreign key constraint "content_type_id_refs_id_522d7533"

After removing the constraint I can also add data like so (without startEditing())

cp.dataProvider().addFeatures([f])
(True, [<qgis._core.QgsFeature object at 0x7fcece585a68>])
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 555)))
f.setAttributes([77, 199, "ref-new", 5])
cp.dataProvider().addFeatures([f])
(True, [<qgis._core.QgsFeature object at 0x7fcece585b00>])  
0

Buenas, tambien tuve el mismo problema, lo que hice para solucionarlo es; en la tabla postgres de donde se saca el registro, agregar una columna geometrica. y listo, se soluciono sin quitar la clave primaria ,


I also had the same problem, what I did to solve it is; in the postgres table where the record is taken, add a geometric column. and ready, it was solved without removing the primary key,

  • 1
    Bienvenido a GIS SE, visita el tour y por favor edita tu respuesta. Este es un sitio de preguntas/respuestas en Inglés, gracias de antemano. – aldo_tapia Sep 12 '17 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.