Trying to create a databases using the flask-SQLAlchemy module. When I insert this code into the command line "from sqlalchemy import db". I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\andre\AppData\Local\Programs\Python\Python38\sqlalchemy.py", line 1
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
^
SyntaxError: invalid syntax.
Here is my code:
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///site.db'
db = SQLAlchemy(app)
class User(db.model):
id = db.Column(db.integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file=
db.Column(db.String(20),nullable=False,default='default.jpg')
password = db.Column(db.String(60), nullabale=False)
posts= db.relationship('Post', backref='author', lazy = True)
def __repr__(self):
return f"User('{self.username}', '{self.email}',
'{self.image_file}')"
class post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'),
nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
Could someone please help me? I've been stuck on this quite some time. I've installed flask-SQLAlchemy and updated my pip. Don't know what else to do really.
You had some typos in the code and not sure regarding that image_file=
line so removed that line for now, follow the below code it would provide u with db
edited: didnt saw that image file one line updated it can you check
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file=db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}','{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
db.create_all() # this create the site.db
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.