In QGIS I'd like to graphically select multiple objects in one layer and insert corresponding multiple IDs in a unique field of a second layer. Is that possible with QGIS? (I'm working with two PostgreSQL/PostGIS tables, the first with geometries and the second without.)

I'll try to explain myself better: in QGIS I'd like to select some points in one layer (with geometries) and copy multiple IDs related in a single field "p_id" of another table (with no geometries) in the form {id1,id2,id3,etc...}, like the following example:

| id | date       | work | p_id           |
| 1  | 2020-04-01 | 1    | {1,2,3}        |
| 2  | 2020-05-02 | 2    | {124,512,99,1} |
| 3  | 2020-12-04 | 1    | {15}           |

Now I would be able to reach this goal by selecting objects, copy-and-paste related ids, separate them by commas, enclose them by curly brackets and write a SQL command to write the values I need in the second table, but I'm asking if I can also insert values starting from a manual graphical selection of objects starting from QGIS

  • 3
    I feel like you don't exactly know what you want to do (or describe it rather poorly), so how should we know what you want to do and then solve the issue?
    – Erik
    Dec 4, 2020 at 8:30
  • If I understand correctly you want to select features from a layer with a geometry. Get the ids of those features, put it in a list and add this list to a field of another layer with no geometries ? How do you know wich list goes with wich feature of the second table ?
    – JULESG
    Dec 4, 2020 at 9:02
  • yes @JULESG I want to do exactly what you wrote. I have to manually select features from first layer, and I need to get the list of relates attribute ids to write in a new record already created in second layer in "p_id" column. No automatic matches. I would tell QGIS the list values after graphical selections.
    – ocelot73
    Dec 4, 2020 at 11:05

1 Answer 1


I have a solution for you but it's in PyQgis. This is the code to put in the text editor of the python console on Qgis. you need to change the name layer_to_change and table_to_change in the first two lines and the name of the column test_to_change :

layer = QgsProject.instance().mapLayersByName("layer_to_change")[0]
table = QgsProject.instance().mapLayersByName("table_to_change")[0]

def listToString(l):
    str1 = "{"  
    for elem in l:  
        str1 += str(elem)
        if elem != l[-1] :
            str1 = str1 + ", "
        else :
            str1 = str1 + "}"
    return str1  

list_result = []
for feature in layer.selectedFeatures():

str_result = listToString(list_result)

for line in table.selectedFeatures():
    table.changeAttributeValue(line.id(), table.fields().indexFromName('test_to_change'), str_result)
feat = QgsFeature(table.fields())
table.changeAttributeValue(feat.id(), table.fields().indexFromName('test'), str_result)


You need to add both your layer and postgis table to a Qgis Project:

enter image description here

Go in the attribute table of your table and select a line you want to modify :

enter image description here

I want to add the list of ID from the layer in the column "test".

Then select the features you want to add to the list of ID on your map canvas :

enter image description here

Once you'vs selected the line and the features you can launch the script. This is the result :

enter image description here

  • 1
    Gorgeus !! I'll test it as soon as I can, in the meanwhile....T H A N K YOU @JULESG
    – ocelot73
    Dec 4, 2020 at 13:37
  • It comes out this error: Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource code = self.compile(source, filename, symbol) File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in call return _maybe_compile(self.compiler, source, filename, symbol) File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile raise err1 File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol)
    – ocelot73
    Dec 4, 2020 at 14:22
  • File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in call codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 3 table.commitChanges() ^ SyntaxError: invalid syntax table.triggerRepaint()
    – ocelot73
    Dec 4, 2020 at 14:24
  • @ocelot73 Maybe try to comment the lines : table.startEditing() table.commitChanges() table.triggerRepaint() and put the table in edition mode before launching the script
    – JULESG
    Dec 4, 2020 at 14:54
  • Your code works @JULESG, thanks.
    – ocelot73
    Dec 5, 2020 at 17:36

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