SQLAlchemy向我显示"AttributeError:类型对象'User'没有属性'columns'"; [英] SQLAlchemy show me that "AttributeError: type object 'User' has no attribute 'columns'"

查看:406
本文介绍了SQLAlchemy向我显示"AttributeError:类型对象'User'没有属性'columns'";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python + Flask + SQLAlchemy构建一个小项目,我制作了以下模型文件:

I am building a small project use python+Flask+SQLAlchemy, I make a model file following:

################# start of models.py #####################
from sqlalchemy import Column, Integer, String, Sequence, Date, DateTime, ForeignKey
from sqlalchemy.orm import relationship, backref
from dk.database import Base
import datetime

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('seq_user_id'), primary_key=True)
    name = Column(String(50), unique=True, index = True, nullable = False)
    email = Column(String(120), unique=True, index = True, nullable = False)
    password = Column(String(128), nullable = False)

    def __init__(self, name, email, password):
        self.name = name
        self.email = email
        self.password = password

    def __repr__(self):
        return '<User %r>' % (self.name)

class Session(Base):
    __tablename__ = 'session'
    id = Column(String(128), primary_key = True, nullable = False)
    user_name = Column(String(30), nullable = False)
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship('User', backref=backref('session', lazy='dynamic'))

    def __repr__(self):
        return '<Session %r>' % (self.id)
################# end of models.py #####################

然后我建立一个初始文件:

and I build a initial file following:

################# start of __init__.py #################
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config') #load database config information
db = SQLAlchemy(app)
################# end of __init__.py #################

当我在脚本中运行"init_db()"时,成功建立到数据库的表. 但是当我想查看SQL脚本时,我在脚本中运行了"print CreateTable(User)",系统显示以下错误:

when I run the "init_db()" in script, tables built to database successful. but when I want to see the SQL script then I run the "print CreateTable(User)" in script, the system show follwing errors:

  File "/home/jacky/flaskcode/venv/lib/python2.6/site-packages/sqlalchemy/schema.py", line 3361, in __init__
    for column in element.columns
AttributeError: type object 'User' has no attribute 'columns'

我不知道如何解决这个问题!

I have no idea how to solve this problem!

推荐答案

您需要为CreateTable()传递 Table 对象:

You need to pass in a Table object for CreateTable():

CreateTable(User.__table__)

但是如果您想查看SQLAlchemy发出的SQL语句,最好通过设置

but if you wanted to see the SQL statements that SQLAlchemy issues you are better off switching on echoing by setting echo=True when creating the connection.

Flask SQLAlchemy集成层支持 SQLALCHEMY_ECHO选项设置该标志.

The Flask SQLAlchemy integration layer supports a SQLALCHEMY_ECHO option to set that flag.

这篇关于SQLAlchemy向我显示"AttributeError:类型对象'User'没有属性'columns'";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆