When I run the flash db migrate, the Foreignkey has failed as follows:
I can't find out what's different about the code.
Please let me know and give me some advice.
DB uses sqlite3.
#Error Contents
File "C:\work\dev\Flask\DB\app.py", line 54, in BlogPost
user_id = db.Column(db.Integer, db.Foreignkey('users.id'))
AttributeError: 'SQLAlchemy' object has no attribute 'Foreignkey'. Did you mean: 'ForeignKey'?
#app.py
importos
from flash import Flash
from flash_sqlalchemy import SQLAlchemy
from flash_migrate import Migrate
from datetime import datetime
from pytz import timezone
app = Flask(__name__)
app.config ['SECRET_KEY'] = 'mysecretkey'
app.config ['SQLALCHEMY_DATABASE_URI'] = 'sqlite://data.sqlite'
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate (app, db)
# Enabling Foreign Key Constraints
from sqlalchemy.engine import Engine
from sqlalchemy import event
@ event.listens_for (Engine, "connect")
default_sqlite_pragma(dbapi_connection, connection_record):
cursor=dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
class User (db.Model):
__tablename__='users'
id=db.Column(db.Integer, primary_key=True)
email=db.Column(db.String(64), unique=True, index=True)
username=db.Column (db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
administrator=db.Column(db.String(1))
# Relationship with blog_post
post=db.relationship('BlogPost', backref='author', lazy='dynamic')
def__init__(self, email, username, password_hash, administrator):
self.email=email
self.username=username
self.password_hash=password_hash
self.administrator=administrator
def__repr__(self):
returnf "UserName: {self.username}"
class BlogPost (db.Model):
__tablename__='blog_post'
id=db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.Foreignkey('users.id'))
date=db.Column(db.DateTime, default=datetime.now(timezone('Asia/Tokyo'))))
title=db.Column(db.String(140))
text=db.Column(db.Text)
summary=db.Column(db.String(140))
featured_image=db.Column(db.String(140))
def_init__(self, title, text, featured_image, user_id, summary):
self.title=title
self.text=text
self.featured_image=featured_image
self.user_id = user_id
self.summary=summary
def__repr__(self):
returnf "PostID: {self.id}, Title: {self.title}, Author: {self.author}\n"
if__name__=='__main__':
app.run(debug=True)
It's written in the error message
'SQLAlchemy' object has no attribute 'Foreignkey'. Did you mean: 'ForeignKey'?
K is lowercase
572 Who developed the "avformat-59.dll" that comes with FFmpeg?
609 GDB gets version error when attempting to debug with the Presense SDK (IDE)
581 PHP ssh2_scp_send fails to send files as intended
578 Understanding How to Configure Google API Key
910 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.