Object Relational Mapping Flashcards

1
Q

Models.py in your django folder is for?

A

Databases!..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the advantage of ORM?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why use this over traditional querying languages?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are models related to ORM?

A

tied in to our models, models are classes that we will write to connect to the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is ORM similar to Flask?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the disadvantage of using ORM? Especially the ORM for Django?

A

When doing very complex queries with where, order by, etc. ORM breaks down and is horrible. Go back to SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Who is in charge of data manipulation in the MTV architecture?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the design principle for Django in terms of controller and models

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the main column types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference in SQL and Django when creating database tables from an ERD diagram?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens with makemigrations? What happens when we migrate?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What special field does Django use for foreign keys?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does Django handle joins?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the equivalent to where in SQL?

A

.filter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you do many to many relationships in Django?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does it matter which model has the many to many key?

A

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.

17
Q

What are validations in Flask?

A

Really just if statement and regex.

18
Q

When doing validations of your data before going to the database, what generally is overwritten in your models.py?

A

models.Manager()

19
Q

Models inheriting from the model class include a property, objects

A

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.

20
Q

What are some differences, from the blog example, between Blog and BlogManager?

A

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

21
Q

What happens when we put an instance of BlogManager(models.Manager) into our our Blog model?

A

It overwrites the old hidden objects key with a new one with EXTRA properties

22
Q

Do we need to migrate when we make changes to Blogmanager?

A

No, it isn not a database table.