|
| 1 | +# UUID (Universally Unique Identifiers) |
| 2 | + |
| 3 | +We have discussed some data types like `str`, `int`, etc. |
| 4 | + |
| 5 | +There's another data type called `UUID` (Universally Unique Identifier). |
| 6 | + |
| 7 | +You might have seen **UUIDs**, for example in URLs. They look something like this: |
| 8 | + |
| 9 | +``` |
| 10 | +4ff2dab7-bffe-414d-88a5-1826b9fea8df |
| 11 | +``` |
| 12 | + |
| 13 | +UUIDs can be particularly useful as an alternative to auto-incrementing integers for **primary keys**. |
| 14 | + |
| 15 | +/// info |
| 16 | + |
| 17 | +Official support for UUIDs was added in SQLModel version `0.0.20`. |
| 18 | + |
| 19 | +/// |
| 20 | + |
| 21 | +## About UUIDs |
| 22 | + |
| 23 | +UUIDs are numbers with 128 bits, that is, 16 bytes. |
| 24 | + |
| 25 | +They are normally seen as 32 <abbr title="numbers in base 16 (instead of base 10), using letters from A to F to represent the numbers from 10 to 15">hexadecimal</abbr> characters separated by dashes. |
| 26 | + |
| 27 | +There are several versions of UUID, some versions include the current time in the bytes, but **UUIDs version 4** are mainly random, the way they are generated makes them virtually **unique**. |
| 28 | + |
| 29 | +### Distributed UUIDs |
| 30 | + |
| 31 | +You could generate one UUID in one computer, and someone else could generate another UUID in another computer, and it would be almost **impossible** for both UUIDs to be the **same**. |
| 32 | + |
| 33 | +This means that you don't have to wait for the DB to generate the ID for you, you can **generate it in code before sending it to the database**, because you can be quite certain it will be unique. |
| 34 | + |
| 35 | +/// note | Technical Details |
| 36 | + |
| 37 | +Because the number of possible UUIDs is so large (2^128), the probability of generating the same UUID version 4 (the random ones) twice is very low. |
| 38 | + |
| 39 | +If you had 103 trillion version 4 UUIDs stored in the database, the probability of generating a duplicated new one is one in a billion. 🤓 |
| 40 | + |
| 41 | +/// |
| 42 | + |
| 43 | +For the same reason, if you decided to migrate your database, combine it with another database and mix records, etc. you would most probably be able to **just use the same UUIDs** you had originally. |
| 44 | + |
| 45 | +/// warning |
| 46 | + |
| 47 | +There's still a chance you could have a collision, but it's very low. In most cases you could assume you wouldn't have it, but it would be good to be prepared for it. |
| 48 | + |
| 49 | +/// |
| 50 | + |
| 51 | +### UUIDs Prevent Information Leakage |
| 52 | + |
| 53 | +Because UUIDs version 4 are **random**, you could give these IDs to the application users or to other systems, **without exposing information** about your application. |
| 54 | + |
| 55 | +When using **auto-incremented integers** for primary keys, you could implicitly expose information about your system. For example, someone could create a new hero, and by getting the hero ID `20` **they would know that you have 20 heroes** in your system (or even less, if some heroes were already deleted). |
| 56 | + |
| 57 | +### UUID Storage |
| 58 | + |
| 59 | +Because UUIDs are 16 bytes, they would **consume more space** in the database than a smaller auto-incremented integer (commonly 4 bytes). |
| 60 | + |
| 61 | +Depending on the database you use, UUIDs could have **better or worse performance**. If you are concerned about that, you should check the documentation for the specific database. |
| 62 | + |
| 63 | +SQLite doesn't have a specific UUID type, so it will store the UUID as a string. Other databases like Postgres have a specific UUID type which would result in better performance and space usage than strings. |
| 64 | + |
| 65 | +## Models with UUIDs |
| 66 | + |
| 67 | +To use UUIDs as primary keys we need to import `uuid`, which is part of the Python standard library (we don't have to install anything) and use `uuid.UUID` as the **type** for the ID field. |
| 68 | + |
| 69 | +We also want the Python code to **generate a new UUID** when creating a new instance, so we use `default_factory`. |
| 70 | + |
| 71 | +The parameter `default_factory` takes a function (or in general, a "<abbr title="Something that can be called as a function.">callable</abbr>"). This function will be **called when creating a new instance** of the model and the value returned by the function will be used as the default value for the field. |
| 72 | + |
| 73 | +For the function in `default_factory` we pass `uuid.uuid4`, which is a function that generates a **new UUID version 4**. |
| 74 | + |
| 75 | +/// tip |
| 76 | + |
| 77 | +We don't call `uuid.uuid4()` ourselves in the code (we don't put the parenthesis). Instead, we pass the function itself, just `uuid.uuid4`, so that SQLModel can call it every time we create a new instance. |
| 78 | + |
| 79 | +/// |
| 80 | + |
| 81 | +This means that the UUID will be generated in the Python code, **before sending the data to the database**. |
| 82 | + |
| 83 | +//// tab | Python 3.10+ |
| 84 | + |
| 85 | +```Python hl_lines="1 7" |
| 86 | +{!./docs_src/advanced/uuid/tutorial001_py310.py[ln:1-10]!} |
| 87 | + |
| 88 | +# Code below omitted 👇 |
| 89 | +``` |
| 90 | + |
| 91 | +//// |
| 92 | + |
| 93 | +//// tab | Python 3.7+ |
| 94 | + |
| 95 | +```Python hl_lines="1 8" |
| 96 | +{!./docs_src/advanced/uuid/tutorial001.py[ln:1-11]!} |
| 97 | + |
| 98 | +# Code below omitted 👇 |
| 99 | +``` |
| 100 | + |
| 101 | +//// |
| 102 | + |
| 103 | +/// details | 👀 Full file preview |
| 104 | + |
| 105 | +//// tab | Python 3.10+ |
| 106 | + |
| 107 | +```Python |
| 108 | +{!./docs_src/advanced/uuid/tutorial001_py310.py!} |
| 109 | +``` |
| 110 | + |
| 111 | +//// |
| 112 | + |
| 113 | +//// tab | Python 3.7+ |
| 114 | + |
| 115 | +```Python |
| 116 | +{!./docs_src/advanced/uuid/tutorial001.py!} |
| 117 | +``` |
| 118 | + |
| 119 | +//// |
| 120 | + |
| 121 | +/// |
| 122 | + |
| 123 | +Pydantic has support for <a href="https://docs.pydantic.dev/latest/api/standard_library_types/#uuid" class="external-link" target="_blank">`UUID` types</a>. |
| 124 | + |
| 125 | +For the database, **SQLModel** internally uses <a href="https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.Uuid" class="external-link" target="_blank">SQLAlchemy's `Uuid` type</a>. |
| 126 | + |
| 127 | +### Create a Record with a UUID |
| 128 | + |
| 129 | +When creating a `Hero` record, the `id` field will be **automatically populated** with a new UUID because we set `default_factory=uuid.uuid4`. |
| 130 | + |
| 131 | +As `uuid.uuid4` will be called when creating the model instance, even before sending it to the database, we can **access and use the ID right away**. |
| 132 | + |
| 133 | +And that **same ID (a UUID)** will be saved in the database. |
| 134 | + |
| 135 | +//// tab | Python 3.10+ |
| 136 | + |
| 137 | +```Python hl_lines="5 7 9 14" |
| 138 | +# Code above omitted 👆 |
| 139 | + |
| 140 | +{!./docs_src/advanced/uuid/tutorial001_py310.py[ln:23-34]!} |
| 141 | + |
| 142 | +# Code below omitted 👇 |
| 143 | +``` |
| 144 | + |
| 145 | +//// |
| 146 | + |
| 147 | +//// tab | Python 3.7+ |
| 148 | + |
| 149 | +```Python hl_lines="5 7 9 14" |
| 150 | +# Code above omitted 👆 |
| 151 | + |
| 152 | +{!./docs_src/advanced/uuid/tutorial001.py[ln:24-35]!} |
| 153 | + |
| 154 | +# Code below omitted 👇 |
| 155 | +``` |
| 156 | + |
| 157 | +//// |
| 158 | + |
| 159 | +/// details | 👀 Full file preview |
| 160 | + |
| 161 | +//// tab | Python 3.10+ |
| 162 | + |
| 163 | +```Python |
| 164 | +{!./docs_src/advanced/uuid/tutorial001_py310.py!} |
| 165 | +``` |
| 166 | + |
| 167 | +//// |
| 168 | + |
| 169 | +//// tab | Python 3.7+ |
| 170 | + |
| 171 | +```Python |
| 172 | +{!./docs_src/advanced/uuid/tutorial001.py!} |
| 173 | +``` |
| 174 | + |
| 175 | +//// |
| 176 | + |
| 177 | +/// |
| 178 | + |
| 179 | +### Select a Hero |
| 180 | + |
| 181 | +We can do the same operations we could do with other fields. |
| 182 | + |
| 183 | +For example we can **select a hero by ID**: |
| 184 | + |
| 185 | +//// tab | Python 3.10+ |
| 186 | + |
| 187 | +```Python hl_lines="15" |
| 188 | +# Code above omitted 👆 |
| 189 | + |
| 190 | +{!./docs_src/advanced/uuid/tutorial001_py310.py[ln:37-54]!} |
| 191 | + |
| 192 | +# Code below omitted 👇 |
| 193 | +``` |
| 194 | + |
| 195 | +//// |
| 196 | + |
| 197 | +//// tab | Python 3.7+ |
| 198 | + |
| 199 | +```Python hl_lines="15" |
| 200 | +# Code above omitted 👆 |
| 201 | + |
| 202 | +{!./docs_src/advanced/uuid/tutorial001.py[ln:38-55]!} |
| 203 | + |
| 204 | +# Code below omitted 👇 |
| 205 | +``` |
| 206 | + |
| 207 | +//// |
| 208 | + |
| 209 | +/// details | 👀 Full file preview |
| 210 | + |
| 211 | +//// tab | Python 3.10+ |
| 212 | + |
| 213 | +```Python |
| 214 | +{!./docs_src/advanced/uuid/tutorial001_py310.py!} |
| 215 | +``` |
| 216 | + |
| 217 | +//// |
| 218 | + |
| 219 | +//// tab | Python 3.7+ |
| 220 | + |
| 221 | +```Python |
| 222 | +{!./docs_src/advanced/uuid/tutorial001.py!} |
| 223 | +``` |
| 224 | + |
| 225 | +//// |
| 226 | + |
| 227 | +/// |
| 228 | + |
| 229 | +/// tip |
| 230 | + |
| 231 | +Even if a database like SQLite stores the UUID as a string, we can select and run comparisons using a Python UUID object and it will work. |
| 232 | + |
| 233 | +SQLModel (actually SQLAlchemy) will take care of making it work. ✨ |
| 234 | + |
| 235 | +/// |
| 236 | + |
| 237 | +#### Select with `session.get()` |
| 238 | + |
| 239 | +We could also select by ID with `session.get()`: |
| 240 | + |
| 241 | +//// tab | Python 3.10+ |
| 242 | + |
| 243 | +```Python hl_lines="15" |
| 244 | +# Code above omitted 👆 |
| 245 | + |
| 246 | +{!./docs_src/advanced/uuid/tutorial002_py310.py[ln:37-54]!} |
| 247 | + |
| 248 | +# Code below omitted 👇 |
| 249 | +``` |
| 250 | + |
| 251 | +//// |
| 252 | + |
| 253 | +//// tab | Python 3.7+ |
| 254 | + |
| 255 | +```Python hl_lines="15" |
| 256 | +# Code above omitted 👆 |
| 257 | + |
| 258 | +{!./docs_src/advanced/uuid/tutorial002.py[ln:38-55]!} |
| 259 | + |
| 260 | +# Code below omitted 👇 |
| 261 | +``` |
| 262 | + |
| 263 | +//// |
| 264 | + |
| 265 | +/// details | 👀 Full file preview |
| 266 | + |
| 267 | +//// tab | Python 3.10+ |
| 268 | + |
| 269 | +```Python |
| 270 | +{!./docs_src/advanced/uuid/tutorial002_py310.py!} |
| 271 | +``` |
| 272 | + |
| 273 | +//// |
| 274 | + |
| 275 | +//// tab | Python 3.7+ |
| 276 | + |
| 277 | +```Python |
| 278 | +{!./docs_src/advanced/uuid/tutorial002.py!} |
| 279 | +``` |
| 280 | + |
| 281 | +//// |
| 282 | + |
| 283 | +/// |
| 284 | + |
| 285 | +The same way as with other fields, we could update, delete, etc. 🚀 |
| 286 | + |
| 287 | +### Run the program |
| 288 | + |
| 289 | +If you run the program, you will see the **UUID** generated in the Python code, and then the record **saved in the database with the same UUID**. |
| 290 | + |
| 291 | +<div class="termy"> |
| 292 | + |
| 293 | +```console |
| 294 | +$ python app.py |
| 295 | + |
| 296 | +// Some boilerplate and previous output omitted 😉 |
| 297 | + |
| 298 | +// In SQLite, the UUID will be stored as a string |
| 299 | +// other DBs like Postgres have a specific UUID type |
| 300 | +CREATE TABLE hero ( |
| 301 | + id CHAR(32) NOT NULL, |
| 302 | + name VARCHAR NOT NULL, |
| 303 | + secret_name VARCHAR NOT NULL, |
| 304 | + age INTEGER, |
| 305 | + PRIMARY KEY (id) |
| 306 | +) |
| 307 | + |
| 308 | +// Before saving in the DB we already have the UUID |
| 309 | +The hero before saving in the DB |
| 310 | +name='Deadpond' secret_name='Dive Wilson' id=UUID('0e44c1a6-88d3-4a35-8b8a-307faa2def28') age=None |
| 311 | +The hero ID was already set |
| 312 | +0e44c1a6-88d3-4a35-8b8a-307faa2def28 |
| 313 | + |
| 314 | +// The SQL statement to insert the record uses our UUID |
| 315 | +INSERT INTO hero (id, name, secret_name, age) VALUES (?, ?, ?, ?) |
| 316 | +('0e44c1a688d34a358b8a307faa2def28', 'Deadpond', 'Dive Wilson', None) |
| 317 | + |
| 318 | +// And indeed, the record was saved with the UUID we created 😎 |
| 319 | +After saving in the DB |
| 320 | +age=None id=UUID('0e44c1a6-88d3-4a35-8b8a-307faa2def28') name='Deadpond' secret_name='Dive Wilson' |
| 321 | + |
| 322 | +// Now we create a new hero (to select it in a bit) |
| 323 | +Created hero: |
| 324 | +age=None id=UUID('9d90d186-85db-4eaa-891a-def7b4ae2dab') name='Spider-Boy' secret_name='Pedro Parqueador' |
| 325 | +Created hero ID: |
| 326 | +9d90d186-85db-4eaa-891a-def7b4ae2dab |
| 327 | + |
| 328 | +// And now we select it |
| 329 | +Selected hero: |
| 330 | +age=None id=UUID('9d90d186-85db-4eaa-891a-def7b4ae2dab') name='Spider-Boy' secret_name='Pedro Parqueador' |
| 331 | +Selected hero ID: |
| 332 | +9d90d186-85db-4eaa-891a-def7b4ae2dab |
| 333 | +``` |
| 334 | + |
| 335 | +</div> |
| 336 | + |
| 337 | +## Learn More |
| 338 | + |
| 339 | +You can learn more about **UUIDs** in: |
| 340 | + |
| 341 | +* The official <a href="https://docs.python.org/3/library/uuid.html" class="external-link" target="_blank">Python docs for UUID</a>. |
| 342 | +* The <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier" class="external-link" target="_blank">Wikipedia for UUID</a>. |
0 commit comments