diff --git a/src/providers/mssql/qgsmssqlprovider.cpp b/src/providers/mssql/qgsmssqlprovider.cpp index 4cf649118868..d09d8157a785 100644 --- a/src/providers/mssql/qgsmssqlprovider.cpp +++ b/src/providers/mssql/qgsmssqlprovider.cpp @@ -1456,7 +1456,7 @@ bool QgsMssqlProvider::addAttributes( const QList &attributes ) for ( QList::const_iterator it = attributes.begin(); it != attributes.end(); ++it ) { QString type = it->typeName(); - if ( type == QLatin1String( "char" ) || type == QLatin1String( "varchar" ) ) + if ( type == QLatin1String( "char" ) || type == QLatin1String( "varchar" ) || type == QLatin1String( "nvarchar" ) ) { if ( it->length() > 0 ) type = QStringLiteral( "%1(%2)" ).arg( type ).arg( it->length() ); diff --git a/tests/src/python/test_provider_mssql.py b/tests/src/python/test_provider_mssql.py index b5774364bca7..053761e03762 100644 --- a/tests/src/python/test_provider_mssql.py +++ b/tests/src/python/test_provider_mssql.py @@ -980,6 +980,23 @@ def test_insert_pk_escaping(self): features = list(vl.getFeatures()) self.assertEqual([f['test-field'] for f in features], [1]) + def test_nvarchar_length(self): + """ + Test that nvarchar length is correctly set + """ + md = QgsProviderRegistry.instance().providerMetadata('mssql') + conn = md.createConnection(self.dbconn, {}) + + conn.execSql('DROP TABLE IF EXISTS qgis_test.test_nvarchar_length') + conn.execSql('CREATE TABLE qgis_test.test_nvarchar_length (id integer PRIMARY KEY)') + + uri = f'{self.dbconn} table="qgis_test"."test_nvarchar_length" sql=' + vl = QgsVectorLayer(uri, '', 'mssql') + + self.assertTrue(vl.isValid()) + self.assertTrue(vl.dataProvider().addAttributes([QgsField('name', QMetaType.Type.QString, 'nvarchar', 12)])) + self.assertEqual(vl.dataProvider().fields().at(1).length(), 12) + if __name__ == '__main__': unittest.main()