Repository: biolab/orange3

Issue:
OWSql does not load the stored tables [with fix]
##### Orange version
3.20.1 (but also 3.19)

##### Expected behavior
When I load an ows file all the SQL Tables automatically reconnect to the server


##### Actual behavior
The SQL Tables do not select the correct tables, and I should open each one in turn to select the proper table


##### Steps to reproduce the behavior
Open an ows file containing a saved SQL Table


##### Additional info (worksheets, data, screenshots, ...)
the problem arises because the TableModel is not yet filled with the tables' names when __init__ select the table. This should be done AFTER connecting to the DB.

Solution: edit the file Orange/widgets/data/owsql.py and in the OWSql.__init__ method move the lines 

```
        index = self.tablecombo.findText(str(self.table))
        if index != -1:
            self.tablecombo.setCurrentIndex(index)
        # set up the callback to select_table in case of selection change
        self.tablecombo.activated[int].connect(self.select_table)
```

at the end of the metod, just after the line 

```
        self.connect()
```



Discussion:
@lanzagar, @robertcv, could one of you try this?
Perhaps there is a more elegant solution :) this is a quick hack

Resolving patch:
diff --git a/Orange/widgets/data/owsql.py b/Orange/widgets/data/owsql.py
--- a/Orange/widgets/data/owsql.py
+++ b/Orange/widgets/data/owsql.py
@@ -141,12 +141,6 @@ def __init__(self):
         self.tablecombo.setToolTip('table')
         tables.layout().addWidget(self.tablecombo)
 
-        index = self.tablecombo.findText(str(self.table))
-        if index != -1:
-            self.tablecombo.setCurrentIndex(index)
-        # set up the callback to select_table in case of selection change
-        self.tablecombo.activated[int].connect(self.select_table)
-
         self.connectbutton = gui.button(
             tables, self, '↻', callback=self.connect)
         self.connectbutton.setSizePolicy(
@@ -181,6 +175,11 @@ def __init__(self):
         gui.rubber(self.buttonsArea)
 
         self.connect()
+        index = self.tablecombo.findText(str(self.table))
+        if index != -1:
+            self.tablecombo.setCurrentIndex(index)
+        # set up the callback to select_table in case of selection change
+        self.tablecombo.activated[int].connect(self.select_table)
 
         QTimer.singleShot(0, self.select_table)
 


Test patch:
diff --git a/Orange/widgets/data/tests/test_owsql.py b/Orange/widgets/data/tests/test_owsql.py
--- a/Orange/widgets/data/tests/test_owsql.py
+++ b/Orange/widgets/data/tests/test_owsql.py
@@ -91,6 +91,28 @@ def test_non_postgres(self, mock_backends):
         self.assertTrue(widget.download)
         self.assertFalse(widget.downloadcb.isEnabled())
 
+    @mock.patch('Orange.widgets.data.owsql.Table')
+    @mock.patch('Orange.widgets.data.owsql.SqlTable')
+    @mock.patch('Orange.widgets.data.owsql.Backend')
+    def test_restore_table(self, mock_backends, mock_sqltable, mock_table):
+        """Test if selected table is restored from settings"""
+        backend = mock.Mock()
+        backend().display_name = "database"
+        del backend().missing_extension
+        backend().list_tables.return_value = ["a", "b", "c"]
+        mock_backends.available_backends.return_value = [backend]
+        mock_sqltable().approx_len.return_value = 100
+
+        settings = {'__version__': 2,
+                    'host': '',
+                    'port': '',
+                    'guess_values': False,
+                    'download': False,
+                    'table': 'b'}
+
+        widget = self.create_widget(OWSql, stored_settings=settings)
+        self.assertEqual(widget.tablecombo.currentText(), "b")
+
 
 if __name__ == "__main__":
     unittest.main()