Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Setting foreign_keys on sqlite is failing #169

@iban-rodriguez

Description

@iban-rodriguez

Hello!

We are trying to activate foreign_keys option on sqlite through a PRAGMA query but it is not being activated. The following code should print Set foreign_keys: (1,) but it always print Set foreign_keys: (0,). It is probably due to not using the same connection for all queries but generating a new one for each of them. Is there a way to overcome this issue?

import asyncio
from databases import Database

async def set_foreign_key(database):
    result = await database.fetch_one("PRAGMA foreign_keys;")
    print("Initial foreign_keys: {}".format(result))
    await database.execute("PRAGMA foreign_keys = ON;")
    result = await database.fetch_one("PRAGMA foreign_keys;")
    print("Set foreign_keys: {}".format(result))

loop = asyncio.get_event_loop()
database = Database("sqlite:///:memory:")
loop.run_until_complete(set_foreign_key(database))
loop.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions