I am developing a Flask-sqlalchemy application and using mySql database. I have a form from where I receive data to be stored in database table named "intent". The table has 2 columns: intent_id & intent_name.
Here is code for my SqlAlchemy Model:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Intent(db.Model):
__tablename__ = 'intent'
intent_id = db.Column('intent_id', db.Integer, primary_key=True)
intent_name = db.Column(db.String(250))
Here is code for my Flask Routes file:
from flask import Flask, render_template, request, session, redirect, url_for
from models import db, Intent, Sentence
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
@app.route("/", methods = ['POST', 'GET'])
def index():
if request.method == 'POST':
intentobj = Intent(intent_name = request.form['intent'])
db.session.add(intentobj)
db.session.commit()
return render_template("index.html", intent_data = Intent.query.all())
elif request.method == 'GET':
return render_template("index.html", intent_data = Intent.query.all())
I am getting this error :
sqlalchemy.exc.IntegrityError: (_mysql_exceptions.IntegrityError) (1364, "Field 'intent_id' doesn't have a default value") [SQL: 'INSERT INTO intent (intent_name) VALUES (%s)'] [parameters: ('def',)]
When I create an object of my Database Table's Class, I only store intent_name in it (as given above). I expect the intent_id to auto-increment and store in database as it's a primary key but it isn't working that way, I guess.
Note that I created the table using phpmyadmin and not through python.
Please help me remove this error. Thanks.