I followed the tutorial to create a simple database-backed flask website. Here
- I successfully followed the tutorial but I'm struggling to create more than one table using this method. I want the table to be called Ingredient.
Here is my code from flask_app.py
from flask import Flask, redirect, render_template, request, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["DEBUG"] = True
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:
{password}@{hostname}/{databasename}".format(
username="Harrryj",
password="mypassword",
hostname="Harrryj.mysql.pythonanywhere-services.com",
databasename="Harrryj$comments",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
class Comment(db.Model):
__tablename__ = "Comment"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(100))
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("main_page.html", comments=Comment.query.all())
comment = Comment(content=request.form["contents"])
db.session.add(comment)
db.session.commit()
return redirect(url_for('index'))
class Ingredient(db.Model):
__tablename__ = "Ingredient"
Ingredient_ID = db.Column(db.Integer, primary_key=True)
Ingredient_Name = db.Column(db.String(100))
Ingredient_Calories = db.Column(db.Integer(100))
When I try and create the table in the database through the bash console this happens:
In [6]: from flask_app import db
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-5825668f6e50> in <module>()
----> 1 from flask_app import db
/home/Harrryj/mysite/flask_app.py in <module>()
41
42
---> 43 class Ingredient(db.Model):
44 __tablename__ = "Ingredient"
45
/home/Harrryj/mysite/flask_app.py in Ingredient()
46 Ingredient_ID = db.Column(db.Integer, primary_key=True)
47 Ingredient_Name = db.Column(db.String(100))
---> 48 Ingredient_Calories = db.Column(db.Integer(100))
49
50
TypeError: object() takes no parameters
Any help would be appreciated! I know I'm missing something