4

I have added a couple of fields to a postgis table which is loaded into a layer in QGIS. When I access the attribute table in QGIS I can see the two new fields but when I run a script (using scriptrunner) to access the layer it does not show the two new fields.

I check the fields in the script by listing them:

print [field.name() for field in layer.pendingFields() ]

but the new fields are not there and I get key errors when I try and access the fields by name or index.

How can a script access something different to the QGIS gui?

QGIS 2.18.7 on a Mac.

  • I just used the code and it worked for me: I get a list storing the layers' names as strings. Maybe the problem comes from other parts of the code? – mgri Jun 18 '17 at 19:14
  • Did you use layer.updateFields()? – Joseph Jun 19 '17 at 9:43
  • No, I did not use updateFields. I added the fields to a postGIS table and then reloaded the layer. There should be no way the script could be able to access the 'old' version as it literally does not exist. I'll keep hacking and update when I figure out what is actually going on. What is frustrating is that I can't see anyway of debugging this. I have done the obvious stuff like reload the table from database, restart QGIS, etc... – Russell Fulton Jun 24 '17 at 23:31
  • I have figured out what is happening but not why. There are two layers with the same name and QGIS is showing just one in the "Layers Panel". My script was picking up the "wrong' on. mgri was right the problem was elsewhere in the code. Some how the older version of the table is lurking 'invisible' to the gui. I am guessing that this is an artefact of the way I added the fields using pgAdmin rather then QGIS. I am open to suggestions on how I should document this here - my feeling is a new question and answer about duplicate layers. – Russell Fulton Jun 25 '17 at 0:14

Your Answer

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

Browse other questions tagged or ask your own question.