__all__ = [
    'create_engine',
]

import sqlalchemy


def create_engine(db_uri, check_same_thread=False, echo=False):
    engine = sqlalchemy.create_engine(
        db_uri,
        echo=echo,
        connect_args={
            'check_same_thread': check_same_thread,
        },
    )

    @sqlalchemy.event.listens_for(engine, 'connect')
    def do_connect(dbapi_connection, _):
        # Stop pysqlite issue commit automatically.
        dbapi_connection.isolation_level = None

    @sqlalchemy.event.listens_for(engine, 'begin')
    def do_begin(connection):
        connection.execute('BEGIN EXCLUSIVE')

    return engine

__all__ = [
    'create_engine',
]

import sqlalchemy


def create_engine(db_uri, check_same_thread=False, echo=False):
    engine = sqlalchemy.create_engine(
        db_uri,
        echo=echo,
        connect_args={
            'check_same_thread': check_same_thread,
        },
    )

    @sqlalchemy.event.listens_for(engine, 'connect')
    def do_connect(dbapi_connection, _):
        # Stop pysqlite issue commit automatically.
        dbapi_connection.isolation_level = None
        # Enable foreign key.
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys = ON")
        cursor.close()

    @sqlalchemy.event.listens_for(engine, 'begin')
    def do_begin(connection):
        connection.execute('BEGIN EXCLUSIVE')

    return engine

Enable foreign key constraint for SQLite
Enable foreign key constraint for SQLite

  __all__ = [
      'create_engine',
  ]
  
  import sqlalchemy
  
  
  def create_engine(db_uri, check_same_thread=False, echo=False):
      engine = sqlalchemy.create_engine(
          db_uri,
          echo=echo,
          connect_args={
              'check_same_thread': check_same_thread,
          },
      )
  
      @sqlalchemy.event.listens_for(engine, 'connect')
      def do_connect(dbapi_connection, _):
          # Stop pysqlite issue commit automatically.
          dbapi_connection.isolation_level = None
+         # Enable foreign key.
+         cursor = dbapi_connection.cursor()
+         cursor.execute("PRAGMA foreign_keys = ON")
+         cursor.close()
  
      @sqlalchemy.event.listens_for(engine, 'begin')
      def do_begin(connection):
          connection.execute('BEGIN EXCLUSIVE')
  
      return engine
  
Enable foreign key constraint for SQLite
## Code Before:
__all__ = [
    'create_engine',
]

import sqlalchemy


def create_engine(db_uri, check_same_thread=False, echo=False):
    engine = sqlalchemy.create_engine(
        db_uri,
        echo=echo,
        connect_args={
            'check_same_thread': check_same_thread,
        },
    )

    @sqlalchemy.event.listens_for(engine, 'connect')
    def do_connect(dbapi_connection, _):
        # Stop pysqlite issue commit automatically.
        dbapi_connection.isolation_level = None

    @sqlalchemy.event.listens_for(engine, 'begin')
    def do_begin(connection):
        connection.execute('BEGIN EXCLUSIVE')

    return engine

## Instruction:
Enable foreign key constraint for SQLite
## Code After:
__all__ = [
    'create_engine',
]

import sqlalchemy


def create_engine(db_uri, check_same_thread=False, echo=False):
    engine = sqlalchemy.create_engine(
        db_uri,
        echo=echo,
        connect_args={
            'check_same_thread': check_same_thread,
        },
    )

    @sqlalchemy.event.listens_for(engine, 'connect')
    def do_connect(dbapi_connection, _):
        # Stop pysqlite issue commit automatically.
        dbapi_connection.isolation_level = None
        # Enable foreign key.
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys = ON")
        cursor.close()

    @sqlalchemy.event.listens_for(engine, 'begin')
    def do_begin(connection):
        connection.execute('BEGIN EXCLUSIVE')

    return engine
