Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:07:59+00:00 2026-05-14T05:07:59+00:00

The new version of SQLite has the ability to enforce Foreign Key constraints, but

  • 0

The new version of SQLite has the ability to enforce Foreign Key constraints, but for the sake of backwards-compatibility, you have to turn it on for each database connection separately!

sqlite> PRAGMA foreign_keys = ON;

I am using SQLAlchemy — how can I make sure this always gets turned on?
What I have tried is this:

engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
engine.execute('pragma foreign_keys=on')

…but it is not working!…What am I missing?

EDIT:
I think my real problem is that I have more than one version of SQLite installed, and Python is not using the latest one!

>>> import sqlite3
>>> print sqlite3.sqlite_version
3.3.4

But I just downloaded 3.6.23 and put the exe in my project directory!
How can I figure out which .exe it’s using, and change it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:07:59+00:00Added an answer on May 14, 2026 at 5:07 am

    I now have this working:

    Download the latest sqlite and pysqlite2 builds as described above: make sure correct versions are being used at runtime by python.

    import sqlite3   
    import pysqlite2 
    print sqlite3.sqlite_version   # should be 3.6.23.1
    print pysqlite2.__path__       # eg C:\\Python26\\lib\\site-packages\\pysqlite2
    

    Next add a PoolListener:

    from sqlalchemy.interfaces import PoolListener
    class ForeignKeysListener(PoolListener):
        def connect(self, dbapi_con, con_record):
            db_cursor = dbapi_con.execute('pragma foreign_keys=ON')
    
    engine = create_engine(database_url, listeners=[ForeignKeysListener()])
    

    Then be careful how you test if foreign keys are working: I had some confusion here. When using sqlalchemy ORM to add() things my import code was implicitly handling the relation hookups so could never fail. Adding nullable=False to some ForeignKey() statements helped me here.

    The way I test sqlalchemy sqlite foreign key support is enabled is to do a manual insert from a declarative ORM class:

    # example
    ins = Coverage.__table__.insert().values(id = 99,
                                        description = 'Wrong',
                                        area = 42.0,
                                        wall_id = 99,  # invalid fkey id
                                        type_id = 99)  # invalid fkey_id
    session.execute(ins) 
    

    Here wall_id and type_id are both ForeignKey()‘s and sqlite throws an exception correctly now if trying to hookup invalid fkeys. So it works! If you remove the listener then sqlalchemy will happily add invalid entries.

    I believe the main problem may be multiple sqlite3.dll’s (or .so) lying around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've released an app with a Core Data sqlite database. In the new version
I am new to SQLite. I downloaded the latest version of SQLite (both shell
I have an SQLite database in the first version of my iPhone application (which
I have an app which has an SQLite database that is stored in ./Library/Caches
I installed a new version of Apache server, I used options like --with-PACKAGE to
In the new version of Bouncy Castle library there are changes in PKCS10CertificationRequest .
Starting with a recent new version of ADT, I've noticed this new attribute on
I just submitted a new version of our app to the Apple app store,
I've a new version of an App that's ' waiting for review' and when
When a new version of a framework or language appears (e.g. .NET 3.5, SQL2008),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.