Cannot flash db migrate

Asked 1 years ago, Updated 1 years ago, 458 views

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)

python sqlite flash

2023-02-15 06:53

1 Answers

It's written in the error message

 'SQLAlchemy' object has no attribute 'Foreignkey'. Did you mean: 'ForeignKey'?

K is lowercase


2023-02-15 07:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.