Click here to Skip to main content
15,301,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a dynamic blog using flask but when i try to connect to the databse i get the error in the title. Here's my code.
Python
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/gamerhaven'
db = SQLAlchemy(app)


class Contacts(db.Model):
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    phone_num = db.Column(db.String(12), nullable=False)
    msg = db.Column(db.String(120), nullable=False)
    date = db.Column(db.String(12), nullable=True)
    email = db.Column(db.String(20), nullable=False)

@app.route("/")
def home():
    return render_template('index.html')


@app.route("/about")
def about():
    return render_template('about.html')


@app.route("/contact", methods = ['GET', 'POST'])
def contact():
    if(request.method=='POST'):
        '''Add entry to the database'''
        name = request.form.get('name')
        email = request.form.get('email')
        phone = request.form.get('phone')
        message = request.form.get('message')
        entry = Contacts(name=name, phone_num = phone, msg = message, date= datetime.now(),email = email )
        db.session.add(entry)
        db.session.commit()
    return render_template('contact.html')


app.run(debug=True)


And this is the error i get.
<pre>C:\Python\python.exe C:/Users/Swapana/PycharmProjects/flask/main.py
Traceback (most recent call last):
  File "C:\Users\admin\PycharmProjects\flask\main.py", line 10, in <module>
    class Contacts(db.Model):
  File "C:\Users\admin\PycharmProjects\flask\main.py", line 12, in Contacts
    name = db.Column(db.String(80), nullable=False)
TypeError: Column() got an unexpected keyword argument 'nullable'


I pretty much only know the basics of python and this error is driving me crazy for the past 2 days as i can't move forward with my project.

Please help.

What I have tried:

I have checked all the code for typos but i didn't find any. I searched for the solution on the internet but i guess no one has encountered a problem like this. I have posted on other similar sites but didn't get any replies.
Posted
Updated 25-Mar-21 0:02am
Comments
CHill60 25-Mar-21 4:38am
   
nullable is only valid when creating the table

See python - How does `nullable=False` work in SQLAlchemy[^]
Saurabh Nadar 25-Mar-21 6:02am
   
Thanks for replying but i found the problem.

1 solution

I found the problem. Somehow “SQLAlchemhy” was not working properly. So i only had to uninstall and reinstall it. Giving this answer so that it could be helpful for someone in future.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900