2017-10-16 55 views
0

我按照教程創建了一個簡單的數據庫支持的網站。 Here如何創建更多表Pythonanywhere

我成功地遵循了教程,但我正在努力使用此方法創建多個表。我希望桌子被稱爲成分。 這裏是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)) 

我的代碼,當我嘗試通過在bash控制檯在數據庫中創建表出現這種情況:

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 

任何幫助,將不勝感激!我知道我錯過了一些東西

回答

1

這裏的教程的原始作者。問題是你有

Ingredient_Calories = db.Column(db.Integer(100)) 

在突出顯示的行中。一個整數沒有大小,所以你應該這樣做:

Ingredient_Calories = db.Column(db.Integer) 

順便說一句,你的時機非常好。我剛剛寫了一個second part to the tutorial,其中包括添加一個額外的表,所以你可能會發現其他有用的東西。

+0

工作!非常感謝 :) – Harrryj