flaskとSQLAlchemyを使っています。
ユーザー情報をデータベースに登録しようとすると、指定したところと別の場所にデータベースのファイルが生成され、データはどこにも保存されていません。

app.pyのコードとディレクトリとデータベース構造、エラー文を添付します。

app.py

```

    from flask import Flask
    from flask import render_template
    from flask import request, redirect, url_for
    from flask_sqlalchemy import SQLAlchemy
    
    # create the app
    app = Flask(__name__)
    
    # create the extension
    db = SQLAlchemy()
    # configure the SQLite database, relative to the app instance folder
    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
    # initialize the app with the extension
    db.init_app(app)
    
    
    # テーブルを定義####################################################
    
    class UserInfo(db.Model):
        __tablename__ = 'user_info'
        UserID = db.Column(db.Integer, primary_key=True)
        User_Name = db.Column(db.String, nullable=False)
        Email = db.Column(db.String, nullable=False)
        Password = db.Column(db.String, nullable=False)
    
    class CommentInfo(db.Model):
        CommentID = db.Column(db.Integer, primary_key=True)
        Comment_Content = db.Column(db.String, nullable=False)
        Comment_Create_Date = db.Column(db.String, nullable=False)
    
    class ThreadInfo(db.Model):
        ThreadID = db.Column(db.Integer, primary_key=True)
        Thread_Name = db.Column(db.String, nullable=False)
        Thread_Content = db.Column(db.String, nullable=False)
        Thread_Create_Date = db.Column(db.String, nullable=False)
    
    
    #Routing##########################################################
    
    @app.route('/')
    def homepage():
        return render_template('homepage.html', \
            homepage = True, \
            title = 'homepage.html')
    
    @app.route('/threadpage/')
    def threadpage():
        return render_template('threadpage.html', \
            threadpage = True, \
            title = 'threadpage.html')
    
    @app.route('/threadcreate/')
    def threadcreate():
        return render_template('thread_create.html', \
            thread_create = True, \
            title = 'thread_create.html')
    
    # ユーザー情報をDBに登録
    @app.route('/userlogin/', methods=['GET', 'POST'])
    def userlogin():
        if request.method == 'POST':
            user_info = UserInfo(
                User_Name = "a", #request.form['User_Name'],
                Email = "b",     #request.form['Email'],
                Password = "c",  #request.form['Password']
            )
            db.session.add(user_info)
            db.session.commit()
            return redirect('/')#redirect(url_for('user_detail', id=user_info.id))
        else:
            return render_template('user_login.html', \
                user_login = True, \
                title = 'user_login.html')
    
    # IDからDBのユーザー情報を取得して表示
    @app.route("/user_<int:id>")
    def user_detail(id):
            user_info = db.get_or_404(UserInfo, id)
            return render_template("user_detail.html", user_info=user_info)
    
    @app.route('/userpage/')
    def userpage():
        return render_template('userpage.html', \
            userpage = True, \
            title = 'userpage.html')
    
    
    #おまじない###############################################################
    if __name__ == "__main__":
            app.run(debug=True)
    
```

ディレクトリ構造

```

    workdir
      blog
        app.py
        instance
          project.db <-これを使いたい
      instance
        project.db <- これが新規作成される、テーブルは作成されず
    
```

データベース構造

```

      comment_info
      thread_info
      user_info
        UserID
        User_Name
        Email
        Password
    
```

エラー

```

    Traceback (most recent call last):
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py", line 1965, in _exec_single_context
        self.dialect.do_execute(
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\default.py", line 921, in do_execute
        cursor.execute(statement, parameters)
    sqlite3.OperationalError: no such table: user_info
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 2190, in wsgi_app
        response = self.full_dispatch_request()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
        rv = self.handle_user_exception(e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
        rv = self.dispatch_request()
             ^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1469, in dispatch_request
        return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)     
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     
      File "C:\Users\user\sixchan4\blog\app.py", line 69, in userlogin
        db.session.commit()
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\orm\scoping.py", line 554, in commit
        return self._proxied.commit()
               ^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\orm\session.py", line 1923, in commit
        trans.commit(_to_root=True)
      File "<string>", line 2, in commit
      File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\orm\state_changes.py", line 139, in _go
        ret_value = fn(self, *arg, **kw)
                    ^^^^^^^^^^^^^^^^^^^^```
    
```
CC BY-SA 4.0
2023-08-30T07:10:49.873
96078
2023-08-31T01:34:08.257
59621
question
python flask
flaskでno-such-tableとエラーが出る
**自己解決**

従来  
カレントディレクトリをworkdirにした状態でblog/app.pyを指定して実行していた

解決方法  
カレントディレクトリをblogにした状態でapp.pyを指定して実行する
CC BY-SA 4.0
2023-08-31T01:34:08.257
96085
2023-08-31T01:34:08.257
59621
96078
answer