首页 > 解决方案 > 为什么我的 Flask SQLite 表没有占用我添加的新列?

问题描述

我对 Flask 相当陌生,并且正在尝试向我的一个表(称为公司)添加新行,在我尝试将一组新数据提交到所述表之前,一切似乎都运行良好。它一直显示为null,我不知道为什么。我已经对不同的表和列执行了相同的步骤,并且效果很好。任何帮助,将不胜感激。

以下是我的逻辑:

@app.route('/addcompany')
@login_required
def addCompany():
    if request.method == 'POST':
        company_name = request.form['companyname']
        company_description = request.form['companydescription']
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            filename = '1200px-No-logo.svg.png'

        if file and EXTENSIONS:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        logo = 'logos/'+filename

        company_website = 'www.google.com' #request.form['companywebsite']


        new_company = companies(description=company_description, company_name=company_name, logo=logo, company_website=company_website)

        try:
            db.session.add(new_company)
            db.session.commit()
            return redirect('/')
        except:
            return 'there was an issue adding the company'

    else:


        return render_template('addcompany.html')

这是数据库。班级:

class companies(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(200), nullable=False)
    company_name = db.Column(db.String(50), nullable=False)
    logo = db.Column(db.String(200), nullable=True)
    company_website = db.Column(db.String(50), nullable=True)
    date_created = db.Column(db.DateTime, default=datetime.utcnow())

    def __ref__(self):
        return '<Company %r>' % self.id

除了 company_website 列之外的所有内容都有效,有什么想法吗?

带有几个测试的数据库

标签: sqliteflasksqlalchemy

解决方案


推荐阅读