使用Python,Flask和SQLAlchemy将两次输入到Postgresql数据库中

时间:2019-03-12 20:47:15

标签: python flask sqlalchemy

我正在编写我的第一个应用程序,并且作为第一步之一,我试图将示例数据添加到数据库中。尽管所有信息都经过正确处理,并在正确的列中进行了两次添加。下面是代码,有什么想法吗?

import os
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

print(os.environ['POST_USERNAME'])
print(os.environ['POST_PASS'])
print(os.environ['POST_PORT'])
print(os.environ['POST_DB'])

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://" + os.environ['POST_USERNAME'] + ":" + os.environ['POST_PASS'] + "@localhost:" + os.environ['POST_PORT'] + "/" + os.environ['POST_DB']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
ma = Marshmallow(app)

class Recipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200))
    ingredients = db.Column(db.String(600))
    directions = db.Column(db.String(1000))

class RecipeSchema(ma.ModelSchema):
    class Meta:
        fields = ("id", "name", "ingredients", "directions")

db.create_all()

new_recipe = Recipe(name="Sandwich", ingredients='{"Ham": "2 slices", "Bread": "2 slices"}', directions="Make sandwich")
db.session.add(new_recipe)
db.session.commit()


if __name__ == '__main__':
    app.run(debug=True)

0 个答案:

没有答案