-1

I was testing "/contact" endpoint of my flask website (which is under development) The contact form stores the "Name" , "Email" and "Message" of the user. Whenever I input any data in the form and submit it ,the following error is raised :

sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1048, "Column 'name' cannot be null")

It suprises me as I am not leaving out the column 'name' blank :/ My question is not the duplicate of this I have already done everything that will help flask to store the data in the database that the user inputs in the '/contact' endpoint. Still I get the error.

Here's the code related to the 'contact' endpoint :-

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_migrate import Migrate



app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql://username:password@localhost/database'
db = SQLAlchemy(app)
migrate = Migrate(app, db)

class Contact(db.Model):
   
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    message = db.Column(db.String(120), nullable=False)
    time = db.Column(db.String(12), nullable=True)
    email = db.Column(db.String(20), nullable=False)
    


@app.route('/contact', methods=['GET', 'POST'])
def contact():
    if (request.method == 'POST'):
        # add entry to the database
        name = request.form.get('name')
        message = request.form.get('message')
        email = request.form.get('email')

        entry = Contact(name=name, message=message, time=datetime.now(),email=email )
        db.session.add(entry)
        db.session.commit()

    return render_template("contact.html") 





if __name__ == '__main__':

    app.run(debug=True)

Here's my database of phpmyadmin : https://i.paste.pics/AMTV9.png

Here's how the contact endpoint looks like : https://i.paste.pics/AMXQC.png

The traceback of the error :

sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1048, "Column 'name' cannot be null")
[SQL: INSERT INTO contact (name, message, time, email) VALUES (%s, %s, %s, %s)]
[parameters: (None, 'Test', datetime.datetime(2020, 11, 9, 20, 8, 48, 2828), 'someone@gmail.com')]
   

Thank you.

Tanish Sarmah
  • 430
  • 5
  • 14

1 Answers1

0

You are trying to insert a contact with name=None when the field "name" in the Contact model has nullable=False.

Zufra
  • 574
  • 1
  • 7
  • 16
  • I didn't enter 'None' in the place of name instead I entered my name but still it shows 'None' in the parameter .I have already mentioned that . – Tanish Sarmah Nov 09 '20 at 14:50
  • Check the body of the POST request. Probably the "name" is null there. – Zufra Nov 09 '20 at 14:50