Object Relational Mapping Flashcards
Models.py in your django folder is for?
Databases!..
What is the advantage of ORM?
ORMs are used to create a language-specific object oriented representation of a table. When tables are objects, attributes of these objects represent the columns in the database. While methods will correspond to common queries
Why use this over traditional querying languages?
The reason that ORMs are useful is so that we can write purse Python code without having to manage long SQL query strings in our logic. You know from experience how ugly SQL queries can get when doing complex selects.
How are models related to ORM?
tied in to our models, models are classes that we will write to connect to the database.
How is ORM similar to Flask?
Instead of writing direct SQL queries, you create objects, link them together . and have Django come up with a SQL query or appropriate query directly
What are the disadvantage of using ORM? Especially the ORM for Django?
When doing very complex queries with where, order by, etc. ORM breaks down and is horrible. Go back to SQL.
Who is in charge of data manipulation in the MTV architecture?
Models - they control which data are released to the controllers. Because of this, the phrase skinny controllers and fat models is often used, and is an important design principle
What is the design principle for Django in terms of controller and models
Any heavy logic including database queries is the purview of a Model. If a Controller (in Django’s case, a View) needs to perform logic or get information from a database, it should use a Model method to do so.
What are the main column types
CharField(max_length), TextField, IntegerField, BooleanFiled, DateTimeField(auto_now_add = True, auto_now=True),
ForeignKey, ManytoManyField, OneToOneField
Used to indicate a relationship between models (anything that would require a JOIN statement in SQL). ForeignKey is for one-to-many relationships and goes in the model on the ‘many’ side. just as the foreign key column goes in the SQL table on the ‘many’ side
What is the difference in SQL and Django when creating database tables from an ERD diagram?
Creating an ERD diagram in Flask didn’t actually create the database table(s). We had to forward engineer the diagram into SQL code, and then rjun that SQL code in a MySQL environment. Instead of using a separate program to do this Django can do the whole job for us with minimal code.
What happens with makemigrations? What happens when we migrate?
It’s kind of a staging. When this command runs Django is looking at the code we wrote, finding any charges, and formulating the correct Python code to move on to the next step.
Migrate actually makes the changes you’ve made. This steps is where the SQL queries are actually built and executed and is equivalent to forward engineering an ERD.
What special field does Django use for foreign keys?
To show a one-to-many relationship between models, Django uses ForeignKey.
For example,
models.ForeignKey(Author, related_name=’books’). This establishes a one-to-many relationship that one author can have many books, and that information about a books’ author can be accessed as some_book.author. To create a record that has this foreign key relationship, you’d pass it into the create functionl, like with any other field:
How does Django handle joins?
Joins in Django happen automatically. If you have a book object, you don’t need to run any additional query to get information abut the author. my_book.author.name
What is the equivalent to where in SQL?
.filter
How do you do many to many relationships in Django?
In order to use a many-to many relationship in SQL, you had to construct a third, linking table with foreign key relationship to the two tables you wanted to connect. Django will do this for you automatically, if your model includes a ManyToManyField.
Does it matter which model has the many to many key?
No, Unlike with a ForeignKey, it doesn’t matter which model has the ManyToManyField. The model would still work if the key is in another field under the other model.
What are validations in Flask?
Really just if statement and regex.
When doing validations of your data before going to the database, what generally is overwritten in your models.py?
models.Manager()
Models inheriting from the model class include a property, objects
The objects include all those ORM query methods that are so convenient, like .all(), .get(), etc. We can extend this functionality using the Manager class.
What are some differences, from the blog example, between Blog and BlogManager?
They are both inheriting from entirely different models. By inheriting from models.Mode, Blog is made into a database table. models.Manager, however, means our BlogManager will inherit from the ORM-building class
What happens when we put an instance of BlogManager(models.Manager) into our our Blog model?
It overwrites the old hidden objects key with a new one with EXTRA properties
Do we need to migrate when we make changes to Blogmanager?
No, it isn not a database table.