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