使用sqlite3对Flask-SQLAlchemy查询进行分段错误

时间:2017-03-01 19:58:07

标签: python flask sqlite sqlalchemy

我正在使用Flask工厂模式,直到最近才知道如何在应用程序上下文中使用数据库对象。现在更清楚地理解了这一点,每当我尝试查询模型时,我的代码就会产生分段错误。

在请求中,我有:

with current_app.app_context():
    eml = current_app.db.session.query(EmailList).all()

我还试图查询EmailList模型:

with current_app.app_context():
    eml = EmailList.query.all()

这两个都来自uWSGI:

!!! uWSGI process 2014 got Segmentation Fault !!!
*** backtrace of 2014 ***
0   uwsgi                               0x000000010111d750 uwsgi_backtrace + 48
1   uwsgi                               0x000000010111dc93 uwsgi_segfault + 51
2   libsystem_platform.dylib            0x00007fffb3ec9bba _sigtramp + 26
3   ???                                 0x00000000000000b0 0x0 + 176
4   libsqlite3.dylib                    0x00007fffb3860038 fillInUnixFile + 568
5   libsqlite3.dylib                    0x00007fffb385fc70 unixOpen + 2192
6   libsqlite3.dylib                    0x00007fffb385bab8 sqlite3BtreeOpen + 2856
7   libsqlite3.dylib                    0x00007fffb3857a21 openDatabase + 1169
8   _sqlite3.so                         0x00000001026d8ac5 pysqlite_connection_init + 509
9   Python                              0x000000010129dad8 _PyType_Lookup + 1398
10  Python                              0x000000010125b75b PyObject_Call + 99
11  _sqlite3.so                         0x00000001026dcd3f init_sqlite3 + 1358
12  Python                              0x00000001012d710c PyEval_EvalFrameEx + 13560
13  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
14  Python                              0x0000000101279531 PyFunction_SetClosure + 826
15  Python                              0x000000010125b75b PyObject_Call + 99
16  Python                              0x00000001012d7045 PyEval_EvalFrameEx + 13361
17  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
18  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
19  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
20  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
21  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
22  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
23  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
24  Python                              0x0000000101279531 PyFunction_SetClosure + 826
25  Python                              0x000000010125b75b PyObject_Call + 99
26  Python                              0x000000010126640c PyMethod_New + 1221
27  Python                              0x000000010125b75b PyObject_Call + 99
28  Python                              0x00000001012a2283 _PyObject_SlotCompare + 5559
29  Python                              0x000000010129dad8 _PyType_Lookup + 1398
30  Python                              0x000000010125b75b PyObject_Call + 99
31  Python                              0x00000001012d67eb PyEval_EvalFrameEx + 11223
32  Python                              0x00000001012da4d5 _PyEval_SliceIndex + 485
33  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
34  Python                              0x00000001012da4d5 _PyEval_SliceIndex + 485
35  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
36  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
37  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
38  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
39  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
40  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
41  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
42  Python                              0x00000001012da4d5 _PyEval_SliceIndex + 485
43  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
44  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
45  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
46  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
47  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
48  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
49  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
50  Python                              0x00000001012da4d5 _PyEval_SliceIndex + 485
51  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
52  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
53  Python                              0x00000001012da442 _PyEval_SliceIndex + 338
54  Python                              0x00000001012d6eae PyEval_EvalFrameEx + 12954
55  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
56  Python                              0x0000000101279531 PyFunction_SetClosure + 826
57  Python                              0x000000010125b75b PyObject_Call + 99
58  Python                              0x00000001012d7045 PyEval_EvalFrameEx + 13361
59  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
60  Python                              0x0000000101279531 PyFunction_SetClosure + 826
61  Python                              0x000000010125b75b PyObject_Call + 99
62  Python                              0x00000001012d7045 PyEval_EvalFrameEx + 13361
63  Python                              0x00000001012d3a1e PyEval_EvalCodeEx + 1617
*** end of backtrace ***
DAMN ! worker 1 (pid: 2014) died :( trying respawn ...
Respawned uWSGI worker 1 (new pid: 2015)

我在 database.py

中创建了db对象
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

我在 models.py

中使用该db对象
from database import db

class EmailList(db.Model):
    # model definition

我在 create_app()调用中启动它,同时在应用程序上下文中创建表:

from database import db
db.init_app(app)

"""Import the models."""
with app.app_context():
    from models import EmailList, BlogPost
    db.create_all()
    app.db = db

据我了解,我应该可以使用:

with current_app.app_context():
    eml = current_app.db.session.query(EmailList).all()

以便访问工厂中的应用程序共享的数据库。

我正在使用sqlite进行开发。在current_app_context中,正确分配了数据库:

What is current_app.db? --> <SQLAlchemy engine='sqlite:////Users/me/myproject/tmp/dev.db'>

如何查询数据库中的模型?我如何破译追踪中发生的事情?

0 个答案:

没有答案