3

Somehow I can't manage to add a feature to my PostgreSQL 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.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Oct 14, 2016 at 9:55
4
  • 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()). Commented Oct 14, 2016 at 9:58
  • just tried that, returns True too but still no data in my layer.... Commented Oct 14, 2016 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. Commented Oct 14, 2016 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. Commented Oct 14, 2016 at 10:53

2 Answers 2

3

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>]) 
answered Oct 14, 2016 at 10:57
1

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,

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Sep 12, 2017 at 20:46
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.