|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "414f0fb5", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Missing and default values" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "id": "28e327d8", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import datajoint as dj\n", |
| 19 | + "import faker\n", |
| 20 | + "fake = faker.Faker()" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "id": "64f6506d", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "fake.profile()" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "schema = dj.Schema('defaults')" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "1f66841e", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "@schema\n", |
| 50 | + "class Person(dj.Manual):\n", |
| 51 | + " definition = \"\"\"\n", |
| 52 | + " person_id : int\n", |
| 53 | + " ---\n", |
| 54 | + " blood_group = \"unknown\" : enum('A+', 'A-', 'AB+', 'AB-', 'B+', 'B-', 'O+', 'O-', \"unknown\")\n", |
| 55 | + " name : varchar(60)\n", |
| 56 | + " \"\"\"" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "id": "f54ddeac", |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "Person.insert1(dict(person_id=3, **fake.profile()), ignore_extra_fields=True)" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "id": "2f610d0c", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "Person()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "id": "4fb4eb3f", |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "Person.insert1(dict(person_id=5, name=\"heywood\"))" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 29, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "Person.insert1(dict(person_id=6, blood_group=None, name=\"Henrietta Lacks\"))" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 30, |
| 101 | + "id": "4e8b7522", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "data": { |
| 106 | + "text/html": [ |
| 107 | + "\n", |
| 108 | + " \n", |
| 109 | + " <style type=\"text/css\">\n", |
| 110 | + " .Table{\n", |
| 111 | + " border-collapse:collapse;\n", |
| 112 | + " }\n", |
| 113 | + " .Table th{\n", |
| 114 | + " background: #A0A0A0; color: #ffffff; padding:4px; border:#f0e0e0 1px solid;\n", |
| 115 | + " font-weight: normal; font-family: monospace; font-size: 100%;\n", |
| 116 | + " }\n", |
| 117 | + " .Table td{\n", |
| 118 | + " padding:4px; border:#f0e0e0 1px solid; font-size:100%;\n", |
| 119 | + " }\n", |
| 120 | + " .Table tr:nth-child(odd){\n", |
| 121 | + " background: #ffffff;\n", |
| 122 | + " color: #000000;\n", |
| 123 | + " }\n", |
| 124 | + " .Table tr:nth-child(even){\n", |
| 125 | + " background: #f3f1ff;\n", |
| 126 | + " color: #000000;\n", |
| 127 | + " }\n", |
| 128 | + " /* Tooltip container */\n", |
| 129 | + " .djtooltip {\n", |
| 130 | + " }\n", |
| 131 | + " /* Tooltip text */\n", |
| 132 | + " .djtooltip .djtooltiptext {\n", |
| 133 | + " visibility: hidden;\n", |
| 134 | + " width: 120px;\n", |
| 135 | + " background-color: black;\n", |
| 136 | + " color: #fff;\n", |
| 137 | + " text-align: center;\n", |
| 138 | + " padding: 5px 0;\n", |
| 139 | + " border-radius: 6px;\n", |
| 140 | + " /* Position the tooltip text - see examples below! */\n", |
| 141 | + " position: absolute;\n", |
| 142 | + " z-index: 1;\n", |
| 143 | + " }\n", |
| 144 | + " #primary {\n", |
| 145 | + " font-weight: bold;\n", |
| 146 | + " color: black;\n", |
| 147 | + " }\n", |
| 148 | + " #nonprimary {\n", |
| 149 | + " font-weight: normal;\n", |
| 150 | + " color: white;\n", |
| 151 | + " }\n", |
| 152 | + "\n", |
| 153 | + " /* Show the tooltip text when you mouse over the tooltip container */\n", |
| 154 | + " .djtooltip:hover .djtooltiptext {\n", |
| 155 | + " visibility: visible;\n", |
| 156 | + " }\n", |
| 157 | + " </style>\n", |
| 158 | + " \n", |
| 159 | + " <b></b>\n", |
| 160 | + " <div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", |
| 161 | + " <table border=\"1\" class=\"Table\">\n", |
| 162 | + " <thead> <tr style=\"text-align: right;\"> <th> <div class=\"djtooltip\">\n", |
| 163 | + " <p id=\"primary\">person_id</p>\n", |
| 164 | + " <span class=\"djtooltiptext\"></span>\n", |
| 165 | + " </div></th><th><div class=\"djtooltip\">\n", |
| 166 | + " <p id=\"nonprimary\">blood_group</p>\n", |
| 167 | + " <span class=\"djtooltiptext\"></span>\n", |
| 168 | + " </div></th><th><div class=\"djtooltip\">\n", |
| 169 | + " <p id=\"nonprimary\">name</p>\n", |
| 170 | + " <span class=\"djtooltiptext\"></span>\n", |
| 171 | + " </div> </th> </tr> </thead>\n", |
| 172 | + " <tbody> <tr> <td>3</td>\n", |
| 173 | + "<td>O+</td>\n", |
| 174 | + "<td>Rachel Wilson</td></tr><tr><td>5</td>\n", |
| 175 | + "<td>unknown</td>\n", |
| 176 | + "<td>heywood</td></tr><tr><td>6</td>\n", |
| 177 | + "<td>unknown</td>\n", |
| 178 | + "<td>Henrietta Lacks</td> </tr> </tbody>\n", |
| 179 | + " </table>\n", |
| 180 | + " \n", |
| 181 | + " <p>Total: 3</p></div>\n", |
| 182 | + " " |
| 183 | + ], |
| 184 | + "text/plain": [ |
| 185 | + "*person_id blood_group name \n", |
| 186 | + "+-----------+ +------------+ +------------+\n", |
| 187 | + "3 O+ Rachel Wilson \n", |
| 188 | + "5 unknown heywood \n", |
| 189 | + "6 unknown Henrietta Lack\n", |
| 190 | + " (Total: 3)" |
| 191 | + ] |
| 192 | + }, |
| 193 | + "execution_count": 30, |
| 194 | + "metadata": {}, |
| 195 | + "output_type": "execute_result" |
| 196 | + } |
| 197 | + ], |
| 198 | + "source": [ |
| 199 | + "Person()" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "id": "83844f42", |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "import pymysql \n", |
| 210 | + "conn = pymysql.connect(user='root', passwd='simple', host='127.0.0.1')\n", |
| 211 | + "cursor = conn.cursor()" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "id": "47686e7a", |
| 218 | + "metadata": {}, |
| 219 | + "outputs": [], |
| 220 | + "source": [ |
| 221 | + "cursor.execute('SHOW CREATE TABLE defaults.person')\n", |
| 222 | + "print(cursor.fetchone()[1])" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": null, |
| 228 | + "id": "e2ac8cd0", |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [], |
| 231 | + "source": [ |
| 232 | + "cursor.execute(\"\"\"\n", |
| 233 | + "INSERT defaults.person (person_id, blood_group, name) VALUES (2, \"O+\", \"anne\")\n", |
| 234 | + "\"\"\")" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": null, |
| 240 | + "id": "7b1c8980", |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [ |
| 244 | + "cursor.execute(\"\"\"\n", |
| 245 | + "INSERT defaults.person (person_id, blood_group) VALUES (4, \"B+\")\n", |
| 246 | + "\"\"\")" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "code", |
| 251 | + "execution_count": 31, |
| 252 | + "id": "3ac55ecb", |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "ename": "IntegrityError", |
| 257 | + "evalue": "(1048, \"Column 'blood_group' cannot be null\")", |
| 258 | + "output_type": "error", |
| 259 | + "traceback": [ |
| 260 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 261 | + "\u001b[0;31mIntegrityError\u001b[0m Traceback (most recent call last)", |
| 262 | + "Cell \u001b[0;32mIn[31], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m cursor\u001b[39m.\u001b[39;49mexecute(\u001b[39m\"\"\"\u001b[39;49m\n\u001b[1;32m 2\u001b[0m \u001b[39mINSERT defaults.person (person_id, blood_group, name) VALUES (1, NULL, \u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mbob\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m)\u001b[39;49m\n\u001b[1;32m 3\u001b[0m \u001b[39m\"\"\"\u001b[39;49m)\n", |
| 263 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/cursors.py:153\u001b[0m, in \u001b[0;36mCursor.execute\u001b[0;34m(self, query, args)\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[39mpass\u001b[39;00m\n\u001b[1;32m 151\u001b[0m query \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmogrify(query, args)\n\u001b[0;32m--> 153\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_query(query)\n\u001b[1;32m 154\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_executed \u001b[39m=\u001b[39m query\n\u001b[1;32m 155\u001b[0m \u001b[39mreturn\u001b[39;00m result\n", |
| 264 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/cursors.py:322\u001b[0m, in \u001b[0;36mCursor._query\u001b[0;34m(self, q)\u001b[0m\n\u001b[1;32m 320\u001b[0m conn \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_get_db()\n\u001b[1;32m 321\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_clear_result()\n\u001b[0;32m--> 322\u001b[0m conn\u001b[39m.\u001b[39;49mquery(q)\n\u001b[1;32m 323\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_do_get_result()\n\u001b[1;32m 324\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrowcount\n", |
| 265 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/connections.py:558\u001b[0m, in \u001b[0;36mConnection.query\u001b[0;34m(self, sql, unbuffered)\u001b[0m\n\u001b[1;32m 556\u001b[0m sql \u001b[39m=\u001b[39m sql\u001b[39m.\u001b[39mencode(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mencoding, \u001b[39m\"\u001b[39m\u001b[39msurrogateescape\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 557\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_execute_command(COMMAND\u001b[39m.\u001b[39mCOM_QUERY, sql)\n\u001b[0;32m--> 558\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_read_query_result(unbuffered\u001b[39m=\u001b[39;49munbuffered)\n\u001b[1;32m 559\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows\n", |
| 266 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/connections.py:822\u001b[0m, in \u001b[0;36mConnection._read_query_result\u001b[0;34m(self, unbuffered)\u001b[0m\n\u001b[1;32m 820\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 821\u001b[0m result \u001b[39m=\u001b[39m MySQLResult(\u001b[39mself\u001b[39m)\n\u001b[0;32m--> 822\u001b[0m result\u001b[39m.\u001b[39;49mread()\n\u001b[1;32m 823\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result \u001b[39m=\u001b[39m result\n\u001b[1;32m 824\u001b[0m \u001b[39mif\u001b[39;00m result\u001b[39m.\u001b[39mserver_status \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n", |
| 267 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/connections.py:1200\u001b[0m, in \u001b[0;36mMySQLResult.read\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1198\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mread\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[1;32m 1199\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m-> 1200\u001b[0m first_packet \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mconnection\u001b[39m.\u001b[39;49m_read_packet()\n\u001b[1;32m 1202\u001b[0m \u001b[39mif\u001b[39;00m first_packet\u001b[39m.\u001b[39mis_ok_packet():\n\u001b[1;32m 1203\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_read_ok_packet(first_packet)\n", |
| 268 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/connections.py:772\u001b[0m, in \u001b[0;36mConnection._read_packet\u001b[0;34m(self, packet_type)\u001b[0m\n\u001b[1;32m 770\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result\u001b[39m.\u001b[39munbuffered_active \u001b[39mis\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[1;32m 771\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result\u001b[39m.\u001b[39munbuffered_active \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n\u001b[0;32m--> 772\u001b[0m packet\u001b[39m.\u001b[39;49mraise_for_error()\n\u001b[1;32m 773\u001b[0m \u001b[39mreturn\u001b[39;00m packet\n", |
| 269 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/protocol.py:221\u001b[0m, in \u001b[0;36mMysqlPacket.raise_for_error\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[39mif\u001b[39;00m DEBUG:\n\u001b[1;32m 220\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39merrno =\u001b[39m\u001b[39m\"\u001b[39m, errno)\n\u001b[0;32m--> 221\u001b[0m err\u001b[39m.\u001b[39;49mraise_mysql_exception(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_data)\n", |
| 270 | + "File \u001b[0;32m/usr/local/lib/python3.9/site-packages/pymysql/err.py:143\u001b[0m, in \u001b[0;36mraise_mysql_exception\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 141\u001b[0m \u001b[39mif\u001b[39;00m errorclass \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 142\u001b[0m errorclass \u001b[39m=\u001b[39m InternalError \u001b[39mif\u001b[39;00m errno \u001b[39m<\u001b[39m \u001b[39m1000\u001b[39m \u001b[39melse\u001b[39;00m OperationalError\n\u001b[0;32m--> 143\u001b[0m \u001b[39mraise\u001b[39;00m errorclass(errno, errval)\n", |
| 271 | + "\u001b[0;31mIntegrityError\u001b[0m: (1048, \"Column 'blood_group' cannot be null\")" |
| 272 | + ] |
| 273 | + } |
| 274 | + ], |
| 275 | + "source": [ |
| 276 | + "cursor.execute(\"\"\"\n", |
| 277 | + "INSERT defaults.person (person_id, blood_group, name) VALUES (1, NULL, 'bob')\n", |
| 278 | + "\"\"\")" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "code", |
| 283 | + "execution_count": null, |
| 284 | + "metadata": {}, |
| 285 | + "outputs": [], |
| 286 | + "source": [] |
| 287 | + } |
| 288 | + ], |
| 289 | + "metadata": { |
| 290 | + "kernelspec": { |
| 291 | + "display_name": "Python 3 (ipykernel)", |
| 292 | + "language": "python", |
| 293 | + "name": "python3" |
| 294 | + }, |
| 295 | + "language_info": { |
| 296 | + "codemirror_mode": { |
| 297 | + "name": "ipython", |
| 298 | + "version": 3 |
| 299 | + }, |
| 300 | + "file_extension": ".py", |
| 301 | + "mimetype": "text/x-python", |
| 302 | + "name": "python", |
| 303 | + "nbconvert_exporter": "python", |
| 304 | + "pygments_lexer": "ipython3", |
| 305 | + "version": "3.9.17" |
| 306 | + } |
| 307 | + }, |
| 308 | + "nbformat": 4, |
| 309 | + "nbformat_minor": 5 |
| 310 | +} |
0 commit comments