sqlalchemy.exc.IntegrityError不断发生,我需要删除db文件并进行迁移以对其进行修复

时间:2018-12-19 00:41:42

标签: flask sqlalchemy flask-sqlalchemy

我一直遇到一个奇怪的错误,我需要删除数据库,删除迁移并重新开始修复它。它运行了一段时间,然后错误再次发生。谁能看到这个问题?

这是错误:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: organisation.phoneWork [SQL: 'INSERT INTO organisation ("orgName", "emailWork", "emailHome", "emailOther", "phoneWork", "phoneMobile", "phoneHome", "backgroundInfo", "postalStreet", "postalTown", "postalState", "postalPostCode") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'] [parameters: ('asdfasdf', '', '', '', '', '', '', '', '', '', '', '')]

这是路线:

@app.route('/organisation_add', methods=['GET', 'POST'])
@login_required
def org_add():
    form = addOrganisationForm()
    if form.validate_on_submit():
        organisation = Organisation(orgName=form.organisationName.data, 
                                    emailHome=form.emailHome.data, emailWork=form.emailWork.data, emailOther=form.emailOther.data,
                                    phoneWork=form.phoneWork.data, phoneMobile=form.phoneMobile.data, phoneHome=form.phoneHome.data, 
                                    backgroundInfo=form.backgroundInfo.data, 
                                    postalStreet=form.postalStreet.data, postalTown=form.postalTown.data,
                                    postalState=form.postalState.data, postalPostCode=form.postalPostCode.data)
        db.session.add(organisation)
        db.session.commit()
        flash('You added a new organisation')
        return redirect(url_for('organisation', orgId=1))
    return render_template('organisation_add.html', title='Add Organisation', form=form)

这是模型:

class Organisation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    orgName = db.Column(db.String(64), index=True, unique=True)
    emailWork = db.Column(db.String(120), index=True, unique=True)
    emailHome = db.Column(db.String(120), index=True, unique=True)
    emailOther = db.Column(db.String(120), index=True, unique=True)
    phoneWork = db.Column(db.String(120), index=True, unique=True)
    phoneMobile = db.Column(db.String(120), index=True, unique=True)
    phoneHome = db.Column(db.String(120), index=True, unique=True)
    backgroundInfo = db.Column(db.String(1024))
    postalStreet = db.Column(db.String(128), index=True)
    postalTown = db.Column(db.String(64), index=True)
    postalState = db.Column(db.String(12), index=True)
    postalPostCode = db.Column(db.String(8), index=True)

显然还有一种形式。

什么可以阻止这种情况发生?我不能一直删除数据库和迁移。任何帮助将不胜感激。

0 个答案:

没有答案