sqlite3.OperationalError: unable to open database file

Hello Guys!

I have a very simple API using flask_tryton. It was developed under Tryton 5.4, and we migrated to Tryton 6.0 on the production and testing servers, where it works perfectly.

However, on my local development environment, I am encountering an error and cannot start it.

Traceback (most recent call last):
  File "/home/lims/src/api_flask/__init__.py", line 70, in <module>
    tryton = Tryton(app)
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/flask_tryton.py", line 51, in __init__
    self.init_app(app)
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/flask_tryton.py", line 66, in init_app
    with Transaction().start(database, user, readonly=True):
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/trytond/transaction.py", line 107, in start
    database = backend.Database(database_name).connect()
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/trytond/backend/sqlite/database.py", line 355, in connect
    factory=SQLiteConnection)
sqlite3.OperationalError: unable to open database file

For the migration of my local development environment, a virtual environment with the same name as the old one was used to avoid changing any files.

I made sure that the Tryton configuration files /home/lims/src/trytond.conf and the config.cfg file used by flask_tryton are being read (by temporarily renaming the files, which triggered a specific error).

I’m very confused

The error is raised because you are connecting to a Sqlite databse which does not exist.

This may be due to some issues:

  • If you have your database on postgresql, the server is not reading the proper config file, so it uses the default values (sqlite)
  • If your database is on postgres it may be created on the wrong path or does not exist. Make sure that a file named database.sqlite (where database is the name of yourdatabase) exists under the directory pointed by the [database] path value of your configuration. If you do not have any configuration setting, the database will be read from the db folderd in the home directory of the user running the server
  • Also make sure that the user running trytond has read permissions on the config files otherwise the file will be ignored.

In order to debug the problem, I will recommend running trytond with verbose flag as the server will print the configuration file used in such mode.

Hope this helps!

Hola Sergi

Se supone que está todo bien, yo corro tryton con un alias en .bash_rc que es este comando:
trytond -d lantos_testing -c ~/src/trytond.conf

lims@debian:~$ pwd
/home/lims

y no tienen ningún problema. Renombré temporalmente los archivos para verificar que eran los correctos

En el trytond.conf tengo estas primeras líneas, y se conecta con postgres sin problema:

[cache]
model = 300

[database]
uri = postgresql://lims:passlims@/

¿Puedes copiarnos el comando que ejecturas cuando te da error? No me queda claro cuando te da el error.

El error es al ejecutar el archivo que tiene el API , init.py con el comando usual:

lims@debian:~/src/api_flask$ /home/lims/src/venv_test/bin/python /home/lims/src/api_flask/init.py

Esto funcionaba antes de pasar a Tryton 6.0 ()y en los serves de producción y testing funcionan, pero corren con gunicorn, en desarrollo local uso el servidorde desarrollo de flask. El contenido del__init__.py comienza acá:

-- coding: utf8 --

import os
from functools import wraps
from datetime import datetime, timedelta
from typing import List
from itertools import chain

from pydantic import BaseModel, ValidationError, validator

from trytond.model.modelstorage import RequiredValidationError
from trytond.transaction import Transaction
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_tryton import Tryton

def create_app(config=None):
‘’‘Create Flask APP’‘’
app = Flask(name)
app.config.from_pyfile(config)

return app

path = os.path.dirname(os.path.realpath(file))
print(path)
conf_file = ‘%s/config.cfg’ % path

app = create_app(conf_file)
app.config[‘JSON_AS_ASCII’] = False
app.config[‘JSON_SORT_KEYS’] = False

Update: For Flask 2.3 and later use this:

app.json.sort_keys = False

CORS(app)

algunas funciones y clases acá

y acá salta el error

tryton = Tryton(app)

Seguramente el problema es que no tienes la variable de entorno TRYTOND_CONFIG apuntando a tu fichero de configuración. Al no encontrar esta variable entonces coje los valores por defecto y por eso te esta intentando ejecutar el SQLite

Tengo esto igual que en producción:

lims@debian:~/src/api_flask$ cat config.cfg
TRYTON_DATABASE = ‘lantos_testing’
TRYTON_USER = 1
TRYTON_CONFIG = ‘/home/lims/src/trytond.conf’
lims@debian:~/src/api_flask$ ls /home/lims/src/trytond.conf
/home/lims/src/trytond.conf

ahora renombro el .conf para verificar que intentó buscarlo:

lims@debian:~/src/api_flask$ ls /home/lims/src/trytond.conf*
/home/lims/src/trytond.conf.renombrado

y ejecuto:
lims@debian:~/src/api_flask$ /home/lims/src/venv_test/bin/python /home/lims/src/api_flask/__init__.py

y sale el mensaje, que me confirma que sí lo intenta leer,pero obviamente lo renombré:
could not load /home/lims/src/trytond.conf

Hola Sergi

Es como decías tuve que hacer en línea de comandos:
export TRYTOND_CONFIG=/home/lims/src/trytond.conf

y lo agregué al .bashrc.

Me confundió que no era necesario antes de hacer la migración de 5.4 a 6.0, y en los servidores de producción y testing no veo dónde está seteada esa variable (yo no hago la administración de los servidores).

Muchas gracias!

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