Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added foreign key constraint support to SQLite #50

Merged
merged 8 commits into from
Apr 26, 2018

Conversation

kzidane
Copy link
Member

@kzidane kzidane commented Apr 26, 2018

@dmalan sqlite ignores foreign key constraints by default and they have to be enabled by hooking into the connect event per http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#foreign-key-support. This PR adds support for optionally respecting foreign key constraints. Users would just have to pass foreign_keys_enabled=True to SQL's constructor.

As an aside this PR also restores the original value for logger.disabled after temporarily disabling it per
89657ea.

Any thoughts?

PS, thanks to Wellington Rampazo who reported issues with foreign keys when using the library!

@kzidane kzidane self-assigned this Apr 26, 2018
src/cs50/sql.py Outdated
@@ -32,12 +33,23 @@ def __init__(self, url, **kwargs):
if not os.path.isfile(matches.group(1)):
raise RuntimeError("not a file: {}".format(matches.group(1)))

# Create engine, raising exception if back end's module not installed
self.engine = sqlalchemy.create_engine(url, **kwargs)
foreign_keys_enabled = kwargs.pop("foreign_keys_enabled", False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment atop this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did foreign_keys_enabled come from, is that your choice of arg names? Or borrowed from SQLAlchemy? If former, should probably be just foreign_keys, for consistency with the PRAGMA directive, http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#foreign-key-support?

src/cs50/sql.py Outdated
# Create engine, raising exception if back end's module not installed
self.engine = sqlalchemy.create_engine(url, **kwargs)

# Whether to enable foreign key constraints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I would change this to an imperative: "Enable foreign key constraints". (The conditional aspect can be inferred from the if.)

src/cs50/sql.py Outdated
# Whether to enable foreign key constraints
if foreign_keys_enabled:
sqlalchemy.event.listen(self.engine, "connect", _on_connect)
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does PEP require a blank line above comment? Not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that was your convention? 😛

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think I've had occasion to write such? I'd just use _connect then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was referring to newline before comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i do put a blank line below the else:.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehm, I actually thought I put a blank line and you were arguing about why it's there 😇 Added.

tests/sql.py Outdated
@@ -107,18 +107,26 @@ class SQLiteTests(SQLTests):
@classmethod
def setUpClass(self):
self.db = SQL("sqlite:///test.db")
self.db1 = SQL("sqlite:///test1.db", foreign_keys_enabled=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreign_keys=?

src/cs50/sql.py Outdated


# http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#foreign-key-support
def _on_connect(dbapi_connection, connection_record):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _on_connect conventional? Or should we just call it _connect so that it matches the event name?


def setUp(self):
self.db.execute("CREATE TABLE cs50(id INTEGER PRIMARY KEY, val TEXT)")

def multi_inserts_enabled(self):
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used anywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, we disabled :)

@dmalan
Copy link
Member

dmalan commented Apr 26, 2018

Good catch. Did a student's code reveal this omission?

@kzidane
Copy link
Member Author

kzidane commented Apr 26, 2018

@dmalan names changed as requested per d5eda23. And yes, a student reported problems with foreign key support, so I tracked it down.

@kzidane kzidane merged commit bdb8128 into develop Apr 26, 2018
@kzidane kzidane deleted the foreign-key-constraint branch April 26, 2018 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants