|
| 1 | + |
| 2 | +This section describes how to work with database schemas without access to the |
| 3 | +original code that generated the schema. These situations often arise when the |
| 4 | +database is created by another user who has not shared the generating code yet |
| 5 | +or when the database schema is created from a programming language other than |
| 6 | +Python. |
| 7 | + |
| 8 | +.. code-block:: python |
| 9 | +
|
| 10 | + import datajoint as dj |
| 11 | +
|
| 12 | +
|
| 13 | +Working with schemas and their modules |
| 14 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 15 | + |
| 16 | +Typically a DataJoint schema is created as a dedicated Python module. This |
| 17 | +module defines a schema object that is used to link classes declared in the |
| 18 | +module to tables in the database schema. As an example, examine the university |
| 19 | +module: `university.py <https://github.com/vathes/db-programming-with-datajoint/blob/master/notebooks/university.py>`_. |
| 20 | + |
| 21 | +You may then import the module to interact with its tables: |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + import university as uni |
| 26 | +
|
| 27 | +*Connecting dimitri\@localhost:3306* |
| 28 | + |
| 29 | +.. code-block:: python |
| 30 | +
|
| 31 | + dj.Diagram(uni) |
| 32 | +
|
| 33 | +.. figure:: virtual-module-ERD.svg |
| 34 | + :align: center |
| 35 | + :alt: query object preview |
| 36 | + |
| 37 | +.. .. raw:: html |
| 38 | +.. :file: virtual-module-ERD.svg |
| 39 | +
|
| 40 | +Note that dj.Diagram can extract the diagram from a schema object or from a |
| 41 | +Python module containing its schema object, lending further support to the |
| 42 | +convention of one-to-one correspondence between database schemas and Python |
| 43 | +modules in a DataJoint project: |
| 44 | + |
| 45 | +``dj.Diagram(uni)`` |
| 46 | + |
| 47 | +is equvalent to |
| 48 | + |
| 49 | +``dj.Diagram(uni.schema)`` |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + # students without majors |
| 54 | + uni.Student - uni.StudentMajor |
| 55 | +
|
| 56 | +.. figure:: StudentTable.png |
| 57 | + :align: center |
| 58 | + :alt: query object preview |
| 59 | + |
| 60 | +.. .. csv-table:: |
| 61 | +.. :file: Student_Table.csv |
| 62 | +.. :widths: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
| 63 | +.. :header-rows: 1 |
| 64 | +
|
| 65 | +Spawning missing classes |
| 66 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 67 | +Now imagine that you do not have access to ``university.py`` or you do not have |
| 68 | +its latest version. You can still connect to the database schema but you will |
| 69 | +not have classes declared to interact with it. |
| 70 | + |
| 71 | +So let's start over in this scenario. |
| 72 | + |
| 73 | +You can may use the ``dj.list_schemas`` function (new in DataJoint 0.12.0) to |
| 74 | +list the names of database schemas available to you. |
| 75 | + |
| 76 | +.. code-block:: python |
| 77 | +
|
| 78 | + import datajoint as dj |
| 79 | + dj.list_schemas() |
| 80 | +
|
| 81 | +*Connecting dimitri@localhost:3306* |
| 82 | + |
| 83 | +*['dimitri_alter','dimitri_attach','dimitri_blob','dimitri_blobs', |
| 84 | +'dimitri_nphoton','dimitri_schema','dimitri_university','dimitri_uuid', |
| 85 | +'university']* |
| 86 | + |
| 87 | +Just as with a new schema, we start by creating a schema object to connect to |
| 88 | +the chosen database schema: |
| 89 | + |
| 90 | +.. code-block:: python |
| 91 | +
|
| 92 | + schema = dj.schema('dimitri_university') |
| 93 | +
|
| 94 | +If the schema already exists, dj.schema is initialized as usual and you may plot |
| 95 | +the schema diagram. But instead of seeing class names, you will see the raw |
| 96 | +table names as they appear in the database. |
| 97 | + |
| 98 | +.. code-block:: python |
| 99 | +
|
| 100 | + # let's plot its diagram |
| 101 | + dj.Diagram(schema) |
| 102 | +
|
| 103 | +.. figure:: dimitri-ERD.svg |
| 104 | + :align: center |
| 105 | + :alt: query object preview |
| 106 | + |
| 107 | +.. .. raw:: html |
| 108 | +.. :file: dimitri-ERD.svg |
| 109 | +
|
| 110 | +You may view the diagram but, at this point, there is no way to interact with |
| 111 | +these tables. A similar situation arises when another developer has added new |
| 112 | +tables to the schema but has not yet shared the updated module code with you. |
| 113 | +Then the diagram will show a mixture of class names and database table names. |
| 114 | + |
| 115 | +Now you may use the ``schema.spawn_missing_classes`` method to spawn classes into |
| 116 | +the local namespace for any tables missing their classes: |
| 117 | + |
| 118 | +.. code-block:: python |
| 119 | +
|
| 120 | + schema.spawn_missing_classes() |
| 121 | + dj.Di(schema) |
| 122 | +
|
| 123 | +.. figure:: spawned-classes-ERD.svg |
| 124 | + :align: center |
| 125 | + :alt: query object preview |
| 126 | + |
| 127 | +.. .. raw:: html |
| 128 | +.. :file: spawned-classes-ERD.svg |
| 129 | +
|
| 130 | +Now you may interact with these tables as if they were declared right here in |
| 131 | +this namespace: |
| 132 | + |
| 133 | +.. code-block:: python |
| 134 | +
|
| 135 | + # students without majors |
| 136 | + Student - StudentMajor |
| 137 | +
|
| 138 | +.. figure:: StudentTable.png |
| 139 | + :align: center |
| 140 | + :alt: query object preview |
| 141 | + |
| 142 | +Creating a virtual module |
| 143 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 144 | +Now ``spawn_missing_classes`` creates the new classes in the local namespace. |
| 145 | +However, it is often more convenient to import a schema with its Python module, |
| 146 | +equivalent to the Python command |
| 147 | + |
| 148 | +.. code-block:: python |
| 149 | +
|
| 150 | + import university as uni |
| 151 | +
|
| 152 | +We can mimick this import without having access to ``university.py`` using the |
| 153 | +``create_virtual_module`` function: |
| 154 | + |
| 155 | +.. code-block:: python |
| 156 | +
|
| 157 | + import datajoint as dj |
| 158 | +
|
| 159 | + uni = dj.create_virtual_module('university.py', 'dimitri_university') |
| 160 | +
|
| 161 | +*Connecting dimitri@localhost:3306* |
| 162 | + |
| 163 | +Now ``uni`` behaves as an imported module complete with the schema object and all |
| 164 | +the table classes. |
| 165 | + |
| 166 | +.. code-block:: python |
| 167 | +
|
| 168 | + dj.Di(uni) |
| 169 | +
|
| 170 | +.. figure:: added-example-ERD.svg |
| 171 | + :align: center |
| 172 | + :alt: query object preview |
| 173 | + |
| 174 | +.. .. raw:: html |
| 175 | +.. :file: added-example-ERD.svg |
| 176 | +
|
| 177 | +.. code-block:: python |
| 178 | +
|
| 179 | + uni.Student - uni.StudentMajor |
| 180 | +
|
| 181 | +.. figure:: StudentTable.png |
| 182 | + :align: center |
| 183 | + :alt: query object preview |
| 184 | + |
| 185 | +``dj.create_virtual_module`` takes optional arguments. |
| 186 | + |
| 187 | +First, ``create_schema=False`` assures that an error is raised when the schema |
| 188 | +does not already exist. Set it to ``True`` if you want to create an empty schema. |
| 189 | + |
| 190 | +.. code-block:: python |
| 191 | +
|
| 192 | + dj.create_virtual_module('what', 'nonexistent') |
| 193 | +
|
| 194 | +.. code-block:: python |
| 195 | +
|
| 196 | + --------------------------------------------------------------------------- |
| 197 | + DataJointError Traceback (most recent call last) |
| 198 | + . |
| 199 | + . |
| 200 | + . |
| 201 | + DataJointError: Database named `nonexistent` was not defined. Set argument create_schema=True to create it. |
| 202 | +
|
| 203 | +
|
| 204 | +The other optional argument, ``create_tables=False`` is passed to the schema |
| 205 | +object. It prevents the use of the schema obect of the virtual module for |
| 206 | +creating new tables in the existing schema. This is a precautionary measure |
| 207 | +since virtual modules are often used for completed schemas. You may set this |
| 208 | +argument to ``True`` if you wish to add new tables to the existing schema. A |
| 209 | +more common approach in this scenario would be to create a new schema object and |
| 210 | +to use the ``spawn_missing_classes`` function to make the classes available. |
| 211 | + |
| 212 | +However, you if do decide to create new tables in an existing tables using the |
| 213 | +virtual module, you may do so by using the schema object from the module as the |
| 214 | +decorator for declaring new tables: |
| 215 | + |
| 216 | +.. code-block:: python |
| 217 | +
|
| 218 | + uni = dj.create_virtual_module('university.py', 'dimitri_university', create_tables=True) |
| 219 | +
|
| 220 | +.. code-block:: python |
| 221 | +
|
| 222 | + @uni.schema |
| 223 | + class Example(dj.Manual): |
| 224 | + definition = """ |
| 225 | + -> uni.Student |
| 226 | + --- |
| 227 | + example : varchar(255) |
| 228 | + """ |
| 229 | +
|
| 230 | +.. code-block:: python |
| 231 | +
|
| 232 | + dj.Di(uni) |
| 233 | +
|
| 234 | +.. figure:: added-example-ERD.svg |
| 235 | + :align: center |
| 236 | + :alt: query object preview |
| 237 | + |
| 238 | +.. .. raw:: html |
| 239 | +.. :file: added-example-ERD.svg |
0 commit comments