AttributeError: 'SQLAlchemy' object has no attribute 'FieldType'


OK

Python, flask and sqlalchemy newbies here. I am writing a small application to collect data (from an excel file) from different groups (departments). Each department will have associated tables and associated fields. Fields will have predefined types (number, regular text, date). My models.py is:

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)

When I try to run the app I get the error:

(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'

I am using my understanding of object oriented design. But I may not fully understand how sqlalchemy does the mapping - I followed Miguel's Flask Mega-Tutorial closely. But I think I'll start defining the field types and go from there. If there is a gap in my understanding, please help me fill it so I can complete this task. Thanks in advance.

Alex Lord Thorson

So you can't recursively create tables SQLfor any database I know of . So this line

ftype = db.Column(db.FieldType)

Given that it doesn't make any sense.

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

Column('mtype_id', ForeignKey('FieldType.id'))

That's what you really want. What this allows you to do is create a reference to a specific instance of FieldType, and use that reference's key in the instance of Field.

If you want a Field to point to more than one FieldType then you will need a many-to-many relationship.

Related


AttributeError: 'SQLAlchemy' object has no attribute 'FieldType'

OK Python, flask and sqlalchemy newbies here. I am writing a small application to collect data (from an excel file) from different groups (departments). Each department will have associated tables and associated fields. Fields will have predefined types (numbe

SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

WiGeeky: I have two tables named usersand permissionsI want to create a relationship between them using the specified table userPermissions. Here's what my code looks like: 类User(Base): __tablename__ ='用户' id =列(Integer,primary_key = True) first_

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

AttributeError: 'SQLAlchemy' object has no attribute 'datetime'

Wayne I'm following a youtube tutorial on using Flask and when I run this code with python -m flask runit it shows this AttributeError: 'SQLAlchemy' object has no attribute 'datetime'. How can I fix this? from flask import Flask, render_template, url_for from

SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

WiGeeky: I have two tables named usersand permissionsI want to create a relationship between them using the specified table userPermissions. Here's what my code looks like: 类User(Base): __tablename__ ='用户' id =列(Integer,primary_key = True) first_

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two