2017-01-31 22 views
0

我正在嘗試將項目插入到我的數據庫中。我在Flask中使用SQLlite和SQLAlchemy,但似乎存在問題。每當我嘗試從cmd手動插入項目時,都會收到錯誤消息。與Flask的一對多SQLAlchemy數據庫中的錯誤

This session's transaction has been rolled back due to a previous exception during flush.

我已經在我的數據庫中實現了一對多的關係,但似乎一直在搞亂。這裏是我的Python代碼:

from flask import Flask, render_template, request, redirect, url_for 
from flask_sqlalchemy import SQLAlchemy 
from flask_bootstrap import Bootstrap 

main = Flask(__name__) 
db = SQLAlchemy(main) 
main.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://YYYYYYY:[email protected]/address' 
main.config['SECRET_KEY'] = 'something-secret' 
Bootstrap(main) 


class Organisation(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    title = db.Column(db.String(80), unique=True) 
    email = db.Column(db.String(40), unique=True) 
    number = db.Column(db.String(40), unique=True) 
    employees = db.relationship('Person', backref='employer', lazy='dynamic') 

    def __init__(self, title, email, number): 
     self.title = title 
     self.email = email 
     self.number = number 



class Person(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    name = db.Column(db.String(60), unique=False) 
    email = db.Column(db.String(40), unique=True) 
    mobile = db.Column(db.String(40), unique=True) 
    employer_id = db.Column(db.Integer, db.ForeignKey('organisation.id')) 

    def __init__(self, name, email, mobile, employer_id): 
     self.name = name 
     self.email = email 
     self.mobile = mobile 
     self.employer_id = employer_id 



@main.route('/', methods=['GET']) 
def index(): 
    result = Person.query.all() 
    org_result = Organisation.query.all() 
    return render_template("index.html", result=result, org_result=org_result) 


@main.route('/additems', methods=['GET']) 
def additems(): 
    return render_template('add.html') 


@main.route('/add', methods=['GET', 'POST']) 
def add(): 
    person = Person(request.form['name'], request.form['email'], request.form['mobile']) 
    db.session.add(person) 
    db.session.commit() 


if __name__ == "__main__": 
    main.run(debug=True) 

如果我必須說實話,我認爲我的問題是某處初始化功能。我試圖改變他們在幾個方面:

1.Adding員工self.employees =員工和直接嘗試輸入一個組織爲: organisation_one=Organisation(title="XX",email="[email protected]",number="3838",employees=person_one)但還以顏色錯誤,即使之前,我可以提交person_one

2.我試過在Person __init__文件中引用employer_id,當我嘗試添加組織標識時,我收到錯誤「無法修改類型」。

我在做什麼錯了一對多的數據庫模型?有人可以幫我嗎?

+0

是這些現有的表在Postgre數據庫?你有沒有試過爲你的數據庫模型指定'__tablename__'? – abigperson

+0

他們是現有的表格,我不太清楚如何爲模型指定'__tablename__'。 – ZombieChowder

+0

只是添加了一些解釋作爲答案。很確定這會讓你整理出來。 – abigperson

回答

1

您的數據庫模型需要這樣的__tablename__屬性:它告訴它它在數據庫中的實際表名是什麼。否則SQLAlchemy不知道如何爲你編寫SQL。

class Organisation(db.Model): 
    __tablename__ = 'organisation' 
    id = db.Column(db.Integer, primary_key=True) 
    title = db.Column(db.String(80), unique=True) 
    email = db.Column(db.String(40), unique=True) 
    number = db.Column(db.String(40), unique=True) 
    employees = db.relationship('Person', backref='employer', lazy='dynamic') 

    def __init__(self, title, email, number): 
     self.title = title 
     self.email = email 
     self.number = number 

還必須在backref引用此表的名稱爲您的Person模型:

db.ForeignKey('organisation.id')) # assuming "organisation" is the table name 

此外,您/add路線是不完整的,並且將導致錯誤:

@main.route('/add', methods=['GET', 'POST']) 
def add(): 
    person = Person(request.form['name'], request.form['email'], request.form['mobile']) 
    db.session.add(person) 
    db.session.commit() 
    # e.g. add some instruction here on what to do... 
    flash('Person %s <%s>added!' % (request.form['name'], request.form['email'])) 
    return redirect(url_for('main.additems')) 
+0

我用附加的代碼重新啓動了數據庫,但是現在當我嘗試添加一個人爲:'person_seven = Person(name =「John」,email =「[email protected]」,mobile =「0393837」,employer_id = organisation_seven)'我收到一個錯誤:__init__有一個意外的關鍵字參數'employer_id'。只要提到我已經成功地向DB提交了'organisation_seven'。 – ZombieChowder

+0

當我不知道數據庫的具體情況時,很難提供幫助,但是如果'organisation_seven'是'Organistion'模型的SQLAlchemy實例,那麼它需要:'employer_id = organisation_seven.id' – abigperson

+0

。感謝Santoro! – ZombieChowder

相關問題