I am trying to add a new item using my web form.
But I get this error. The email is not null though which I verified from a print statement.
at this line
File "/Users/j/udacity/item_catalog/item-catalog-190/application.py", line 131, in newItem
session.commit()
IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: category.email [SQL: u'INSERT INTO category (name, email) VALUES (?, ?)'] [parameters: (u'Pilates', None)]
@app.route('/catalog/new', methods=['GET','POST'])
def newItem():
if request.method == 'POST':
placeholder=request.form['category']
category = Category(name=placeholder)
print "**********", login_session['email']
email = login_session['email']
newThing = Item(name=request.form['name'], description=request.form['description'], price=request.form['price'],category=category, email=email)
session.add(newThing)
session.commit()
return redirect('/catalog')
else:
return render_template('newitem.html')
These are my two tables.
class Item(Base):
__tablename__ = 'item'
name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
description = Column(String(250))
price = Column(String(8))
category_id = Column(Integer, ForeignKey('category.id'))
email = Column(String(250),nullable=False)
category = relationship(Category)
class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
email = Column(String(250), nullable=False)
Your category table requires the email field to be NOT NULL, and when you create a new category:
category = Category(name=placeholder)
The default value of the email field is NULL.
This is the query for the INSERT:
SQL: u'INSERT INTO category (name, email) VALUES (?, ?)'] [parameters: (u'Pilates', None)
As you can see, the second paramenter (which is the email) is None (which translates to null in SQL.
You might wanna change your code to:
if request.method == 'POST':
placeholder=request.form['category']
email = login_session['email'] # moved the email here
print "**********", login_session['email']
category = Category(name=placeholder, email=email) # here you have the email variable so you can use it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With