-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add foreign key support for sqlite #94
Changes from 4 commits
d0c8a1f
fb79068
b3c29a0
7523c52
d87c14e
5ad6c33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import contextlib | ||
import sqlite3 | ||
|
||
from sqlalchemy import ( | ||
Column, Integer, Boolean, Unicode, ForeignKey, create_engine, Enum, | ||
literal) | ||
literal, event) | ||
from sqlalchemy.engine import Engine | ||
from sqlalchemy.exc import OperationalError | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker, relationship | ||
|
@@ -72,16 +74,17 @@ class Accounting(Base): | |
__tablename__ = "accounting" | ||
|
||
user_id = Column(Integer, | ||
ForeignKey("user.id"), | ||
ForeignKey("user.id", ondelete="CASCADE"), | ||
primary_key=True) | ||
|
||
application_id = Column(Integer, | ||
ForeignKey("application.id"), | ||
ForeignKey("application.id", ondelete="CASCADE"), | ||
primary_key=True) | ||
|
||
application_policy_id = Column(Integer, | ||
ForeignKey("application_policy.id"), | ||
primary_key=True) | ||
application_policy_id = Column( | ||
Integer, | ||
ForeignKey("application_policy.id", ondelete="CASCADE"), | ||
primary_key=True) | ||
|
||
user = relationship("User") | ||
|
||
|
@@ -90,6 +93,14 @@ class Accounting(Base): | |
application_policy = relationship("ApplicationPolicy") | ||
|
||
|
||
@event.listens_for(Engine, "connect") | ||
def set_sqlite_pragma(dbapi_connection, connection_record): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import sqlite3 here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call it _enable_foreign_keys() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the function name |
||
if type(dbapi_connection) is sqlite3.Connection: | ||
cursor = dbapi_connection.cursor() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with contextlib.closing(cursor): |
||
cursor.execute("PRAGMA foreign_keys=ON") | ||
cursor.close() | ||
|
||
|
||
class Database(LoggingMixin): | ||
def __init__(self, url, **kwargs): | ||
"""Initialises a database connection to a given database url. | ||
|
@@ -107,6 +118,7 @@ def __init__(self, url, **kwargs): | |
|
||
self.log.info("Creating session to db: {}".format(self.url)) | ||
self.engine = create_engine(self.url, **kwargs) | ||
|
||
try: | ||
self.session_class = sessionmaker(bind=self.engine) | ||
except OperationalError: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep it encapsulated in the enabling method, so that it works regardless if we have/use sqlite or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqlite3
is in the standard library...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but not necessarily compiled in. You can disable it (at least it was optional in py27, I don't know if it is in py34)