1
00:00:00,325 --> 00:00:01,541
- [Instructor] Hi and welcome back.

2
00:00:01,541 --> 00:00:03,292
In this video, we're going to simplify

3
00:00:03,292 --> 00:00:07,459
our entire code base slightly
more by using SQLAlchemy.

4
00:00:08,624 --> 00:00:09,941
Did you know that SQLAlchemy

5
00:00:09,941 --> 00:00:12,858
can create our data.db file for us?

6
00:00:14,207 --> 00:00:16,550
Well, if you didn't, now you do.

7
00:00:16,550 --> 00:00:19,075
We no longer need the create table script,

8
00:00:19,075 --> 00:00:21,874
so I'm just gonna delete that

9
00:00:21,874 --> 00:00:23,359
and I'm also going to delete data.db

10
00:00:23,359 --> 00:00:25,041
just so you'll believe me

11
00:00:25,041 --> 00:00:29,267
when I say that SQLAlchemy
will create it for us.

12
00:00:29,267 --> 00:00:31,032
In our app.py however,

13
00:00:31,032 --> 00:00:32,849
we of course have to tell SQLAlchemy

14
00:00:32,849 --> 00:00:34,491
to create the database for us.

15
00:00:34,491 --> 00:00:36,991
It won't just magically do it.

16
00:00:38,358 --> 00:00:39,950
Although actually that's a bit of a lie.

17
00:00:39,950 --> 00:00:42,657
It does create it but it
doesn't create any tables in it,

18
00:00:42,657 --> 00:00:44,475
so then that's not very useful.

19
00:00:44,475 --> 00:00:46,357
It creates the data.db file

20
00:00:46,357 --> 00:00:49,140
but it doesn't put anything inside it,

21
00:00:49,140 --> 00:00:50,650
so then you'll get the errors

22
00:00:50,650 --> 00:00:53,499
where the database doesn't exist.

23
00:00:53,499 --> 00:00:54,450
So what we're gonna do here

24
00:00:54,450 --> 00:00:57,692
is we're going to use a Flask decorator.

25
00:00:57,692 --> 00:00:59,966
We're moving backwards to some extent.

26
00:00:59,966 --> 00:01:01,042
We're not really moving backwards

27
00:01:01,042 --> 00:01:03,812
but we're going a bit
backwards in our knowledge

28
00:01:03,812 --> 00:01:07,382
and using something that
comes directly from Flask

29
00:01:07,382 --> 00:01:10,057
as opposed to any of the extensions.

30
00:01:10,057 --> 00:01:14,224
So what we're gonna do is
@app.before_first_request

31
00:01:15,625 --> 00:01:16,475
and this is a decorator

32
00:01:16,475 --> 00:01:19,583
that is going to affect
the method below it

33
00:01:19,583 --> 00:01:21,466
and it's going to run that method

34
00:01:21,466 --> 00:01:24,716
before the first request into this app.

35
00:01:27,231 --> 00:01:30,741
And this method is going
to be called create_tables

36
00:01:30,741 --> 00:01:34,908
and all it's going to do is
it's going to say db.create_all.

37
00:01:37,116 --> 00:01:41,042
Now what is gonna happen is
that before the first request,

38
00:01:41,042 --> 00:01:42,700
no matter what request that is,

39
00:01:42,700 --> 00:01:45,100
but before the first request runs,

40
00:01:45,100 --> 00:01:46,974
it's gonna run this

41
00:01:46,974 --> 00:01:51,217
and that is going to create
this file there, data.db,

42
00:01:51,217 --> 00:01:53,667
and it's gonna create all
of the tables in the file

43
00:01:53,667 --> 00:01:55,834
unless they exist already.

44
00:01:56,750 --> 00:01:59,283
Naturally, if we were using PostgreSQL,

45
00:01:59,283 --> 00:02:01,116
it would also create all the tables there

46
00:02:01,116 --> 00:02:04,907
unless they exist already, and so on.

47
00:02:04,907 --> 00:02:07,858
So now what we can do is go to Postman.

48
00:02:07,858 --> 00:02:09,967
In fact, to make sure
that your code is running,

49
00:02:09,967 --> 00:02:11,632
which it is in my case,

50
00:02:11,632 --> 00:02:14,450
go to Postman and you can,
for example, register,

51
00:02:14,450 --> 00:02:18,107
and notice how that took
slightly longer to run

52
00:02:18,107 --> 00:02:21,500
and indeed you can see
that data.db is now here

53
00:02:21,500 --> 00:02:24,000
and SQLAlchemy has created it.

54
00:02:24,925 --> 00:02:26,407
So that's it.

55
00:02:26,407 --> 00:02:28,349
That's how you can create the tables there

56
00:02:28,349 --> 00:02:29,432
and you no longer need to worry

57
00:02:29,432 --> 00:02:32,217
about running the create table script.

58
00:02:32,217 --> 00:02:35,928
Just include that and the
tables will be created for you.

59
00:02:35,928 --> 00:02:37,391
That's everything for this video

60
00:02:37,391 --> 00:02:39,377
and I'll see you on the next one.

