{"id":16,"date":"2021-10-21T17:24:18","date_gmt":"2021-10-21T17:24:18","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/rayls\/?p=16"},"modified":"2021-10-21T17:24:18","modified_gmt":"2021-10-21T17:24:18","slug":"orms-object-relational-mappers","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/rayls\/2021\/10\/21\/orms-object-relational-mappers\/","title":{"rendered":"ORMs: Object-relational Mappers"},"content":{"rendered":"\n<p>Object-relational mappers are libraries that help programmers map the database data into objects that are more easily usable in their programming language of choice. By abstracting away the database, programmers can then use their programming language to create their tables and access or modify the data in them. There is no need to write a query in your database language, which can help to speed up the development process.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Django&#8217;s ORM<\/h2>\n\n\n\n<p>Django, a popular Python backend framework, offers an ORM that can speed up your web development. With Django\u2019s ORM, you can create models which will be used to generate the tables in your database, and you can use the functions in the database API to then access and modify data in those tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Models<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/usermodel-1024x546.png\" alt=\"\" class=\"wp-image-17\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/usermodel-1024x546.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/usermodel-300x160.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/usermodel-768x409.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/usermodel.png 1036w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Each model is a Python class and contains data fields which will be the columns in the table. Django provides a wide variety of DataFields that map to the various data types available in some popular SQL languages. The DataFields can also be passed parameters so that you can set a max length for a char datatype just like you would in SQL for instance.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Querying your Database<\/h2>\n\n\n\n<p>The database API provided by Django allows you to perform CRUD operations without ever touching SQL as well. We will use the example model from above to write some sample queries to show how this works.&nbsp;<\/p>\n\n\n\n<p><strong>Create<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"323\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer-1024x323.png\" alt=\"\" class=\"wp-image-18\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer-1024x323.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer-300x94.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer-768x242.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer-1200x378.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/adducer.png 1524w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Creates a user with the name Tyler Rayls.<\/figcaption><\/figure>\n\n\n\n<p><strong>Retrieve<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"408\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/retrieve-user.png\" alt=\"\" class=\"wp-image-19\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/retrieve-user.png 940w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/retrieve-user-300x130.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/retrieve-user-768x333.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><figcaption>Since we have only made one user, we can get the only user with the name Tyler.<\/figcaption><\/figure>\n\n\n\n<p><strong>Update<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/updateuser.png\" alt=\"\" class=\"wp-image-20\" width=\"610\" height=\"458\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/updateuser.png 638w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/updateuser-300x226.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><figcaption>Changes the user&#8217;s name from Tyler Rayls to John Doe.<\/figcaption><\/figure>\n\n\n\n<p><strong>Delete<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"462\" height=\"408\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/deleteuser.png\" alt=\"\" class=\"wp-image-21\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/deleteuser.png 462w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4761\/files\/2021\/10\/deleteuser-300x265.png 300w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><figcaption>Deletes the only user in our database. <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>ORMs are a useful tool because they can help speed up web development. They do this by abstracting away the database so that programmers can work in their language of choice and access their data in objects they are familiar with.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object-relational mappers are libraries that help programmers map the database data into objects that are more easily usable in their programming language of choice. By abstracting away the database, programmers can then use their programming language to create their tables and access or modify the data in them. There is no need to write a [&hellip;]<\/p>\n","protected":false},"author":11633,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/users\/11633"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/posts\/16\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/rayls\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}