Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让sqlalchemy拉动整个列_Python_Sql_Sqlite_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python 让sqlalchemy拉动整个列

Python 让sqlalchemy拉动整个列,python,sql,sqlite,sqlalchemy,flask-sqlalchemy,Python,Sql,Sqlite,Sqlalchemy,Flask Sqlalchemy,在常规sql中,我可以通过select*从表中提取整个列,但在sqlalchemy中找不到等效项 我正在尝试从我的用户表中提取所有用户名。到目前为止,我只找到models.User.query.All,然后执行for循环来拉取我想要的列,但我相信这是一种糟糕的做法,因为当我只需要一列时,它会拉取整个表 models.py class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db

在常规sql中,我可以通过select*从表中提取整个列,但在sqlalchemy中找不到等效项

我正在尝试从我的用户表中提取所有用户名。到目前为止,我只找到models.User.query.All,然后执行for循环来拉取我想要的列,但我相信这是一种糟糕的做法,因为当我只需要一列时,它会拉取整个表

models.py

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(120), index=True, unique=True)
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    #mobilitys = db.relationship('Mobility', backref='username', lazy='dynamic')
    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime)

    def __repr__(self):  # pragma: no cover
        return '<user> %r' % (self.username)
正在尝试处理_实体的终端

>>> from app import db, models
>>> models.User.query.with_entities(User.username)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'User' is not defined
>>> models.User.username
<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7fccb4f682f0>
后续问题的终端视图:

jsnyder10@jsnyder10-VirtualBox:~$ cd Documents/45
jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ source env/bin/activate
(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ flask/bin/python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from models import User
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named models
>>> from .models import User
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
第二终端视图:

(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ flask/bin/python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db, models
>>> from models import User
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named models
>>> from .models import User
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
您可以尝试使用方法来限制要在结果中返回的列。例如:

result = User.query.with_entities(User.username)
或使用sqlalchmey会话:

result = session.query(User.username)

所以我在我的终端上试过,它声称用户是未定义的。为了演示它是如何定义的,我随后运行了User.username。我在原始问题的底部发布了结果。您没有导入用户,这就是为什么没有定义第二个用户的原因,您还可以添加以下模型:models.User.query.with_entitiesmodels.User.username,或者从模型导入用户,以便您可以直接使用用户。当我尝试从模型导入用户时,它会给我导入错误:没有名为模型的模块。当我尝试从.models导入用户进行导入时,它会给我ValueError:尝试在非包中进行相对导入。有没有办法解决这个问题,这样我就可以玩终端的代码了?你有没有从app import db,models之前从models import User?我已经试过了,它给我这个错误ImportError:没有名为models的模块。在我的代码中,当我导入它时,我使用from.models导入用户。然而,终端不允许我这样做。