OSError: Database does not exist when running Flask API with Tryton

I am trying to run a Flask API using Tryton, but I am encountering the following error when starting the application:

OSError: Database '/home/username/db/database.sqlite' doesn't exist!

I have set the TRYTON_DATABASE configuration to 'database', but it seems like the application cannot find the database file at the specified location (/home/username/db/database.sqlite).

Here is the relevant part of my Flask app:

from flask import Flask, request, jsonify
from trytond.pool import Pool
from trytond.config import config
from trytond.transaction import Transaction
from flask_tryton import Tryton

app = Flask(__name__)
app.config['TRYTON_DATABASE'] = 'database'
tryton = Tryton(app, configure_jinja=True)

config.update_etc('/home/username/project/backoffice/tryton/trytond.conf')  
Pool.start()

@app.before_request
def log_request():
    print(f"=== Request masuk: {request.method} {request.path} ===")

@app.route('/')
@tryton.transaction()
def hello():
     user, = User.search([('login', '=', 'admin')])
     return '%s, Hello World!' % user.name

I have verified that the database file doesn’t exist at /home/username/db/database.sqlite. What are the recommended steps to either create the database or ensure that the application connects to the correct database?

Additionally, if the database file needs to be created, what would be the correct procedure for setting it up with Tryton?

The trytond configuration must be loaded before flask_tryton is intialized.
The simplest way is to set the environment TRYTON_CONFIG.

Sorry, I didn’t quite understand. Do you mean that I need to run trytond -c trytond.conf first?

You need to set the environment variable TRYOND_CONFIG with the path of your configuration file. This way the flask server will read the settings from this file and connect to your postgres database. Something like:

$ TRYTOND_CONFIG=/home/username/project/backoffice/tryton/trytond.conf flask run

Otherwise the flask server uses the default database settings and tries to connect to a sqlite database.

app = Flask(__name__)

app.config['TRYTOND_CONFIG'] = '/home/username/tryton/trytond.conf'

app.config['TRYTON_DATABASE'] = 'database'

tryton = Tryton(app, configure_jinja=True)

set the TRYTOND_CONFIG environment variable as instructed, but I’m still encountering the same error.

Is there anything else I should check or configure to ensure Flask is correctly reading the Tryton settings?

It is TRYTON_CONFIG that I instructed.

from flask import Flask, request, jsonify
from trytond.pool import Pool
from trytond.config import config
from trytond.transaction import Transaction
from trytond.protocols.wrappers import with_pool, with_transaction
import traceback
from flask_tryton import Tryton



app = Flask(__name__)
app.config['TRYTON_CONFIG'] = '/home/username/tryton/trytond.conf'
app.config['TRYTON_DATABASE'] = 'database'
tryton = Tryton(app, configure_jinja=True)

# debugger
@app.before_request
def log_request():
    print(f"=== Request masuk: {request.method} {request.path} ===")

@app.route('/')
@tryton.transaction()
def hello():
     user, = User.search([('login', '=', 'admin')])
     return '%s, Hello World!' % user.name 

@app.route("/api/hello", methods=["GET"])
def hello():
    print("=== API /api/hello dipanggil! ===")  # Jika ini tidak muncul, berarti request tidak sampai ke sini
    return jsonify({"message": "Hello from Flask!"})

@app.route("/database/login", methods=['POST'])
@with_transaction()
def login():

I did this but it still get this error

(env) user@pop-os:~/tryton/modules/api$ python api_tryton.py
Traceback (most recent call last):
  File "/home/username/tryton/modules/api/api_tryton.py", line 16, in <module>
    tryton = Tryton(app, configure_jinja=True)
  File "/home/username/.virtualenvs/env/lib/python3.10/site-packages/flask_tryton.py", line 52, in __init__
    self.init_app(app)
  File "/home/username/.virtualenvs/env/lib/python3.10/site-packages/flask_tryton.py", line 67, in init_app
    with Transaction().start(database, user, readonly=True):
  File "/home/username/tryton/trytond/trytond/transaction.py", line 107, in start
    database = backend.Database(database_name).connect()
  File "/home/username/tryton/trytond/trytond/backend/sqlite/database.py", line 353, in connect
    self._make_uri(), uri=True,
  File "/home/username/tryton/trytond/trytond/backend/sqlite/database.py", line 425, in _make_uri
    raise IOError("Database '%s' doesn't exist!" % db_path)
OSError: Database '/home/username/db/database.sqlite' doesn't exist!

You must not import trytond stuff before having flask_tryton initialized.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.