<div dir="ltr"><div>Hi ! I'm happy to join this list. Hope you can help with this nasty bug, two days I'm on it...</div><div><br></div><div>Regards,</div><div><br></div><div>Jésahel</div><div> </div><div>**Describe the bug**<br>For a plugin, I need a way to save my layers in a geopackage with their own style, exactly like the "Package" Tool.<br>The problem arises when two or more layers are saved. In the destination gpkg, the layer_styles table contains one row by layer, the styleName field is OK but the f_table_name field contains another layer name. I'm almost sure that the bug is in the saveStyleToDatabase() function. I've tried everything, almost reproducing the [Package tool function](<a href="https://github.com/qgis/QGIS/blob/master/src/analysis/processing/qgsalgorithmpackage.cpp">https://github.com/qgis/QGIS/blob/master/src/analysis/processing/qgsalgorithmpackage.cpp</a>)<br><br>**How to Reproduce**<br>Create two layers with their own style named test and test 2 (or download this one [<a href="http://djes.free.fr/qgis/test.gpkg](http://djes.free.fr/qgis/test.gpkg)">http://djes.free.fr/qgis/test.gpkg](http://djes.free.fr/qgis/test.gpkg)</a>). Adjust the script to the layers name and destination geopackage. Launch the script for the first layer, destination file is OK. For the second, the style is not correctly saved.<br><br>**QGIS and OS versions**<br>3.16.5 and 3.18.1 / Windows 10  <br></div><div><br></div>Simplified code (or download it on <a href="http://djes.free.fr/qgis/saveLayerWithStyle.py">http://djes.free.fr/qgis/saveLayerWithStyle.py</a>)<br>[code]<br>layer_name = 'test' #change me<br>gpkg_name = 'c:\\temp\\mytest.gpkg' #change me if needed<div><br># First, saves the layer</div><div>src_layer = QgsProject.instance().mapLayersByName(layer_name)[0]<br>options = QgsVectorFileWriter.SaveVectorOptions()<br>options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer<br>options.layerName = layer_name<br>#options.SymbologyExport = QgsVectorFileWriter.FeatureSymbology<br>#options.driverName = "GPKG"<br>error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options)<br><br>if error[0] == QgsVectorFileWriter.ErrCreateDataSource:<br>    print("Create mode")<br>    options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile #Create mode<br>    error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options)<br>del options<br>if error[0] == QgsVectorFileWriter.NoError:<br>    print('Layer "' + layer_na<br><br>me + '" saved in "' + gpkg_name + '"')        <br>else:<br>    print(error)<br>    raise Exception('Failed to save layer')<br><br># Second, saves the style</div><div># Load just saved layer<br></div><div>dst_layer = QgsVectorLayer(gpkg_name, layer_name)<br>if not dst_layer.isValid():<br>    raise Exception('Failed to load layer')<br>#print(dst_layer)<br><br>myDocument = QDomDocument('qgis')<br>src_layer.exportNamedStyle(myDocument)<br>#print(myDocument.toString())<br>success, message = dst_layer.importNamedStyle(myDocument)<br>dst_layer.saveStyleToDatabase(layer_name, '', True, '')<br>QgsProject.instance().removeMapLayer(dst_layer)</div><div>del src_layer<br>del dst_layer<br>del myDocument<br>[/code]<div><br><br></div></div></div>