93857


flask db migrateを実行すると以下のようにForeignkeyでエラーとなってしまっています。  
コードのどこが違うのか、調べても調べても分からない状態です。

ご教示、アドバイスの程、お願い致します。

DBは、sqlite3を使っております。

[code]

    # エラー内容
    
      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'?
    
[/code]

[code]

    # app.py
    
    import os
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    from flask_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)
    
    # 外部キー制約有効化
    from sqlalchemy.engine import Engine
    from sqlalchemy import event
    
    @event.listens_for(Engine, "connect")
    def set_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))
        # 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):
            return f"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):
            return f"PostID: {self.id}, Title: {self.title}, Author: {self.author} \n"
    
     
    if __name__ == '__main__':
        app.run(debug=True)
    
    
[/code]


CC BY-SA 4.0
2023-02-15T12:52:48.643
93856
2023-02-15T13:04:50.917
question
python sqlite flask
flask db migrateができない


エラーメッセージに書いてありますよ

[code]

    'SQLAlchemy' object has no attribute 'Foreignkey'. Did you mean: 'ForeignKey'?
    
[/code]

Kが小文字になってます


CC BY-SA 4.0
2023-02-15T13:04:50.917
93857
2023-02-15T13:04:50.917
29037
93856
answer