ODBC Driver 17 not found in deployed back but works in localhost

  Kiến thức lập trình

I hope I am not repeating a post, I haven’t found a solution to my problem yet. I am a junior dev and I am currently trying to push in production a new version of an existing website. I work in python in version 3.9.13 and I try to access to the database (on Azure) through Azure functions.
When I work on my localhost, everything is fine but when I connect my local front to the deployed dev version of the back, I have this error appearing:

('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")

As per procedure, it tries 5 times to connect to the database then crashes.
The same event appears when I am on the deployed front dev version of the website connected to the deployed back dev version. It crashes an Err 500.

I don’t understand why because I already downloaded the correct version of the ODCB on my computer, it works on the running main branch, it is in the requirements.txt document that is loaded during the deployment. I haven’t touched it but here is how the connexion to the DB is coded:

def connect_to_database(server, database, driver, DEBUG_MODE):
        logging.info("ENTERING connect_to_database")
        logging.info(f"Driver: {driver}")
        logging.info(f"Database: {database}")
        logging.info(f"Server: {server}")
        conn = None

        retry_flag = True
        retry_count = 0
        connection_string = (
            f"Driver={driver};Server=tcp:{server};PORT=xxxx;Database={database};"
            "Encrypt=yes;TrustServerCertificate=no;Connection Timeout=5;"
        )

        if DEBUG_MODE:
            connection_string += "[email protected];Authentication=ActiveDirectoryInteractive"
        else:
            connection_string += "Authentication=ActiveDirectoryMsi"

        while retry_flag and retry_count < 5:
            try:
                logging.info("Attempting to connect to database...")
                pyodbc.pooling = True
                conn = pyodbc.connect(connection_string)
                retry_flag = False
                logging.info("Database connection successful.")
            except Exception as e:
                logging.error(f"Retry {retry_count + 1} failed: {e}")
                retry_count += 1
                time.sleep(1)

        if conn:
            logging.info("DB connection OK")
        else:
            logging.error("Failed to connect to the database after retries")

        return conn

    def connect_to_MPAPPS():
        logging.info("ENTERING connect_to_MPAPPS")
        conn = connect_to_MPAPPS_Conn()
        if conn:
            logging.info(f"conn result: {conn}")
            cursor = conn.cursor()
            logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS")
            return cursor
        else:
            logging.error("Failed to obtain a database connection")
            return None

    def connect_to_MPAPPS_Conn():
        logging.info("ENTERING connect_to_MPAPPS_Conn")
        server = "server address"
        logging.info(f"Server: {server}")
        database = "database_name"
        logging.info(f"Database: {database}")
        driver = "{ODBC Driver 17 for SQL Server}"
        logging.info(f"Driver: {driver}")

        DEBUG_MODE = 1 if os.getenv("AZURE_FUNCTIONS_ENVIRONMENT") == "Development" else 0
        conn = connect_to_database(server, database, driver, DEBUG_MODE)
        logging.info(f"conn: {conn}")
        logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS_Conn")
        return conn

I tried console.logging everything I could to have a better understanding of the issue, I tried to call from Postman, I searched everywhere, I checked all the variables I have access to in Azure, I checked if the SQL Driver was the same as the main (it is), I can see it downloaded on my computer, I haven’t foud a different variable between main and dev…

Please don’t hesitate to ask any questions if the info I provided is incomplete.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT