Let's check this example of a simple SQL query to get all the data from the hero table:
SELECT*FROMhero;
And that SQL query would return the table:
id
name
secret_name
age
team_id
1
Deadpond
Dive Wilson
null
2
2
Spider-Boy
Pedro Parqueador
null
1
3
Rusty-Man
Tommy Sharp
48
1
This SQL language has a little caveat. It was not designed to be mixed with normal code in a programming language like Python. 🚨
So, if you are working with Python, the simplest option would be to put SQL code inside a string, and send that string directly to the database.
statement="SELECT * FROM hero;"results=database.execute(statement)
But in that case, you wouldn't have editor support, inline errors, autocompletion, etc. Because for the editor, the SQL statement is just a string of text. If you have an error, the editor wouldn't be able to help. 😔
And even more importantly, in most of the cases, you would send the SQL strings with modifications and parameters. For example, to get the data for a specific item ID, a range of dates, etc.
And in most cases, the parameters your code uses to query or modify the data in the database come, in some way, from an external user.
For example, check this SQL query:
SELECT*FROMheroWHEREid=2;
It is using the ID parameter 2. That number 2 probably comes, in some way, from a user input.
The user is probably, in some way, telling your application:
Hey, I want to get the hero with ID:
2
And the result would be this table (with a single row):
But let's say that your code takes whatever the external user provides and puts it inside the SQL string before sending it to the database. Something like this:
# Never do this! 🚨 Continue reading.user_id=input("Type the user ID: ")statement=f"SELECT * FROM hero WHERE id = {user_id};"results=database.execute(statement)
If the external user is actually an attacker, they could send you a malicious SQL string that does something terrible like deleting all the records. That's called a "SQL Injection".
For example, imagine that this new attacker user says:
Hey, I want to get the hero with ID:
2;DROPTABLEhero
Then the code above that takes the user input and puts it in SQL would actually send this to the database:
SELECT*FROMheroWHEREid=2;DROPTABLEhero;
Check that section added at the end. That's another entire SQL statement:
DROPTABLEhero;
That is how you tell the database in SQL to delete the entire table hero.
Nooooo! We lost all the data in the hero table! 💥😱
Relational: refers to the SQL Databases. Remember that they are also called Relational Databases, because each of those tables is also called a "relation"? That's where the "Relational" comes from.
For example this Relation or table:
id
name
secret_name
age
team_id
1
Deadpond
Dive Wilson
null
2
2
Spider-Boy
Pedro Parqueador
null
1
3
Rusty-Man
Tommy Sharp
48
1
Mapper: this comes from Math, when there's something that can convert from some set of things to another, that's called a "mapping function". That's where the Mapper comes from.
We could also write a mapping function in Python that converts from the set of lowercase letters to the set of uppercase letters, like this:
This is a bit of boring background for SQL purists. Feel free to skip this section. 😉
When working with pure SQL, it's common to name the tables in plural. So, the table would be named heroes instead of hero, because it could contain multiple rows, each with one hero.
Nevertheless, SQLModel and many other similar tools can generate a table name automatically from your code, as you will see later in the tutorial.
But this name will be derived from a class name. And it's common practice to use singular names for classes (e.g. class Hero, instead of class Heroes). Using singular names for classes like class Hero also makes your code more intuitive.
You will see your own code a lot more than the internal table names, so it's probably better to keep the code/class convention than the SQL convention.
So, to keep things consistent, I'll keep using the same table names that SQLModel would have generated.
Tip
You can also override the table name. You can read about it in the Advanced User Guide.