这里有Python,烧瓶,还有炼金术新手。我正在编写一个小应用程序来收集来自不同组(部门)的数据(从excel文件)。每个部门都将有关联的表和相关的字段。字段将具有预定义的类型(数字、常规文本、日期)。我的models.py是:
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)当我试图运行应用程序时,有一个错误:
(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教程中得到的。但我想我应该开始定义字段类型,然后从那里开始构建。如果我的理解有差距,请帮助我填补它,这样我就能完成这个任务。提前谢谢。
发布于 2015-09-12 08:45:56
因此,您不能递归地为我所知道的任何数据库在SQL中创建表。所以这条线
ftype = db.Column(db.FieldType)考虑到这一点没有任何意义。
Without more information I'm going to assume that you want a many to one relationship
Column('mtype_id', ForeignKey('FieldType.id'))是你在这种情况下真正想要的。这允许您创建对特定FieldType实例的引用,并在Field实例中使用对该引用的键。
https://stackoverflow.com/questions/32535653
复制相似问题