Python for the .NET developer Transcripts
Chapter: Database access and ORMs in Python
Lecture: Fixing the Sqlite objects created in another thread error
0:00 A quick followup to that error that we were just seeing.
0:02 It turns out I made a super subtle mistake.
0:05 There's a big bug on this page, do you see it?
0:09 Yeah, I didn't either.
0:10 Let's run it real quick just so we can see the error again.
0:13 We come over here and we run this.
0:16 Click around, it still works
0:20 except for that bit right there.
0:22 But let's click on these pieces.
0:23 Nah, I'll open 'em up, whatever.
0:24 I've been fiddling with it
0:25 and it's kind of broken a little bit
0:27 but we should see these errors here
0:29 about your SQLAlchemy type.
0:32 Well, I changed one thing
0:35 which made it run into a problem which is different.
0:39 So I will show you over here on this
0:41 when we close the context I said, you know what?
0:43 Actually, when we leave the width block
0:46 there should really be no session
0:47 so let's make sure the Garbage Collector cleans it up.
0:50 Not this, let's run it over here
0:52 so you can see it more clearly.
0:53 But now that I put it back the way it was
0:55 you'll see if I'm clicking around
0:58 that is shouldn't have ever gotten that error, right?
1:00 We never tried to access the database
1:02 out of the database context, did we?
1:04 Well, what did we get?
1:06 We get these crazy errors here.
1:08 And that was the bug.
1:09 Yes, we actually were accessing
1:14 outside of the context 'cause see this little line?
1:17 That is the boundary of the context manager
1:20 and this, I had indented the wrong way.
1:23 There we go. Now, very subtly, you should be able to see.
1:27 Yep, it's inside the context manager
1:29 and later we return it.
1:30 If we just save that, it should run again.
1:33 Notice it saw that there were some changes.
1:37 And let's go and actually clear that.
1:38 And we go click around
1:39 we'll see a bunch of requests coming in.
1:43 First of all, our None AttributeError thing went away.
1:46 There was like a no reference exception, basically.
1:49 And notice, no more errors.
1:51 Okay, the problem was we had to close the session
1:54 and then we were trying to do more work with it
1:56 which was causing issues.
1:58 Oddly, it was working, but it was saying
1:59 you know, not so much though.
2:02 Like this is going to cause trouble.
2:03 Anyway, sorry about that mistake.
2:05 Just make sure that this is within the with block.
2:08 Obviously, once we get the data just into memory
2:11 we can do all sorts of stuff outside the with block there.