首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError: SQLAlchemy对象没有属性“FieldType”

AttributeError: SQLAlchemy对象没有属性“FieldType”
EN

Stack Overflow用户
提问于 2015-09-12 05:53:15
回答 1查看 2.6K关注 0票数 0

这里有Python,烧瓶,还有炼金术新手。我正在编写一个小应用程序来收集来自不同组(部门)的数据(从excel文件)。每个部门都将有关联的表和相关的字段。字段将具有预定义的类型(数字、常规文本、日期)。我的models.py是:

代码语言:javascript
复制
from app import db
from sqlalchemy.schema import Sequence, CreateSequence

class FieldType(db.Model):

    __tablename__ = 'fieldtype'

    id = db.Column(db.Integer, db.Sequence('ftypeid'), primary_key = True)
    name = db.Column(db.String(120))
    python_type = db.Column(db.String(120))
    python_import = db.Column(db.String(120))

class Field(db.Model):

    __tablename__ = 'field'

    id = db.Column(db.Integer, db.Sequence('fieldid'), primary_key = True)
    name = db.Column(db.String(120), index = True, unique = True)
    position = db.Column(db.Integer)
    ftype = db.Column(db.FieldType) #The error is happening here.
    tableinfoid = db.Column(db.Integer, db.ForeignKey('tableinfo.id'))

class TableInfo(db.Model):
    __tablename__ = 'tableinfo'

    id = db.Column(db.Integer, db.Sequence('tinfoid'), primary_key = True)
    name = db.Column(db.String(30), index = True, unique = True)
    fields = db.relationship('Field', backref='parent_table', lazy='dynamic')

class Department(db.Model):
    __tablename__ = 'department'

    id = db.Column(db.Integer, db.Sequence('dpartmntid'), primary_key = True)
    name = db.Column(db.String(120), index = True, unique = True)

当我试图运行应用程序时,有一个错误:

代码语言:javascript
复制
(env)[vagrant@localhost dataupload]$ ./run.py
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import theapp
File "/home/vagrant/Development/dataupload/app/__init__.py", line 13, in <module>
from app import views, models
File "/home/vagrant/Development/dataupload/app/models.py", line 13, in <module>
class Field(db.Model):
File "/home/vagrant/Development/dataupload/app/models.py", line 20, in Field
ftype = db.Column(db.FieldType)     #The error is happening here.
AttributeError: 'SQLAlchemy' object has no attribute 'FieldType'

我正在使用我对面向对象设计的理解。但是我可能不完全理解sqlalchemy是如何进行映射的--我是从Miguel的Flask教程中得到的。但我想我应该开始定义字段类型,然后从那里开始构建。如果我的理解有差距,请帮助我填补它,这样我就能完成这个任务。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-12 08:45:56

因此,您不能递归地为我所知道的任何数据库在SQL中创建表。所以这条线

代码语言:javascript
复制
ftype = db.Column(db.FieldType)

考虑到这一点没有任何意义。

Without more information I'm going to assume that you want a many to one relationship

代码语言:javascript
复制
Column('mtype_id', ForeignKey('FieldType.id'))

是你在这种情况下真正想要的。这允许您创建对特定FieldType实例的引用,并在Field实例中使用对该引用的键。

If you want to be able to have a Field point to many FieldTypes than you'll need a many to many relationship.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32535653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档