<div dir="ltr">Hi,<div><br></div><div>First, configuration :</div><div>QGIS 3.16.4 on Windows 10</div><div>Database : SQL Server 11</div><div><br></div><div>For a long time I have had problems with Python and retrieving default style in the database.</div><div><br></div><div>I digged around and found that our layer style definition in the [layer_styles] table was:</div><div><div style="line-height:16px"><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">---</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">SET</span> <span style="color:rgb(0,0,255)">ANSI_NULLS</span> <span style="color:rgb(0,0,255)">ON</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">GO</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">SET</span> <span style="color:rgb(0,0,255)">QUOTED_IDENTIFIER</span> <span style="color:rgb(0,0,255)">ON</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">GO</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">CREATE</span> <span style="color:rgb(0,0,255)">TABLE</span> [dbo].[layer_styles](</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [qgs_fid] [int] <span style="color:rgb(0,0,255)">IDENTITY</span>(<span style="color:rgb(9,136,90)">1</span>,<span style="color:rgb(9,136,90)">1</span>) <span style="color:rgb(0,0,255)">NOT</span> <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [id] [int] <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [f_table_catalog] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [f_table_schema] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [f_table_name] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [f_geometry_column] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [styleName] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [styleQML] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [styleSLD] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [useAsDefault] [int] <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [description] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [owner] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [ui] [nvarchar](<span style="color:rgb(121,94,38)">max</span>) <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [update_time] [datetime] <span style="color:rgb(0,0,255)">NULL</span>,</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"> <span style="color:rgb(0,0,255)">CONSTRAINT</span> [PK_layer_styles] <span style="color:rgb(0,0,255)">PRIMARY</span> <span style="color:rgb(0,0,255)">KEY</span> <span style="color:rgb(0,0,255)">CLUSTERED</span> </div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">(</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">    [qgs_fid] <span style="color:rgb(0,0,255)">ASC</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">)<span style="color:rgb(0,0,255)">WITH</span> (<span style="color:rgb(0,0,255)">PAD_INDEX</span> <span style="color:rgb(0,0,0)">=</span> <span style="color:rgb(0,0,255)">OFF</span>, <span style="color:rgb(0,0,255)">STATISTICS_NORECOMPUTE</span> <span style="color:rgb(0,0,0)">=</span> <span style="color:rgb(0,0,255)">OFF</span>, <span style="color:rgb(0,0,255)">IGNORE_DUP_KEY</span> <span style="color:rgb(0,0,0)">=</span> <span style="color:rgb(0,0,255)">OFF</span>, <span style="color:rgb(0,0,255)">ALLOW_ROW_LOCKS</span> <span style="color:rgb(0,0,0)">=</span> <span style="color:rgb(0,0,255)">ON</span>, <span style="color:rgb(0,0,255)">ALLOW_PAGE_LOCKS</span> <span style="color:rgb(0,0,0)">=</span> <span style="color:rgb(0,0,255)">ON</span>) <span style="color:rgb(0,0,255)">ON</span> [PRIMARY]</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)">) <span style="color:rgb(0,0,255)">ON</span> [PRIMARY] <span style="color:rgb(0,0,255)">TEXTIMAGE_ON</span> [PRIMARY]</div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">GO</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)">---</span></div><div style="color:rgb(33,33,33);font-family:Consolas,"Courier New",monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,254)"><span style="color:rgb(0,0,255)"><br></span></div><div style=""><span style="background-color:rgb(255,255,255)">
As you can see, the primary key is [qgis_fid]. </span></div><div style=""><span style="background-color:rgb(255,255,255)"><br></span></div><div style="">I've looked in QGIS source code and listStylesInDatabase() and other functions need the primary key to be [id]... I have no idea where this [qgis_fid] is coming from, maybe some past import/export.</div><div style=""><br></div><div style="">This issue creates several problems :</div><div style="">- Saving default style creates a new record each time instead of replacing the default one.</div><div style="">- listStylesInDatabase() returns a list without any clue about the default style (the first being not the good one as all id == null ). And there isn't a way to know what is the default style (this function lacks a default list).  </div><div style=""><br></div><div style="">So instead of hanging around in python with the table, I prefered to modify the table so that the primary key be [id]. I hope there won't be glitches coming from this but it seems to work.</div><div style=""><br></div><div style="">If it could help,</div><div style=""><br></div><div style="">Jésahel</div></div></div></div>