Jonathan LaCour <[EMAIL PROTECTED]> writes:
> Thanks for the complements! I wrote this a while back, and I was
> definitely still learning a lot about TurboGears, so I was hoping
> that there weren't too many shameful mistakes in the code ;)
It is not a mistake but an optimization your might want to have: there are no
indices in your model. If you use indices, then you'll be able to get an
answer for your queries much faster than without them.
For example, I'd do:
================================================================================
class Issue(SQLObject):
issue = UnicodeCol()
impact = IntCol()
closed = BoolCol(default=False)
project = ForeignKey('Project')
creator = ForeignKey('Person')
creation_date = DateTimeCol(default=datetime.now)
idxProject = DatabaseIndex('project')
idxCreator = DatabaseIndex('creator')
================================================================================
This would allow searches by project and by creator using indices.
You can also have indices with multiple columns or specifying a set of columns
to be unique, etc. This might alleviate some of your logic.
================================================================================
# Example of a unique index on project, creator and creationDate:
idxUniqProjectCreatorCreationDate = DatabaseIndex('project', 'creator',
'creation_date',
unique = True)
================================================================================
I also try using sqlmeta and setting lazyUpdate to True, to avoid contacting
the database when each column changes. With this setting, changes are only
sent to the database when I call the sync() method. So, the above would
become:
================================================================================
class Issue(SQLObject):
class sqlmeta:
lazyUpdate = True
issue = UnicodeCol()
impact = IntCol()
closed = BoolCol(default=False)
project = ForeignKey('Project')
creator = ForeignKey('Person')
creation_date = DateTimeCol(default=datetime.now)
idxProject = DatabaseIndex('project')
idxCreator = DatabaseIndex('creator')
================================================================================
And when I want to edit some Issue:
================================================================================
issue = model.Issue.get(issue_id)
issue.impact = new_impact
issue.closed = False
issue.sync()
================================================================================
Only one UPDATE should be sent making both changes. If you're submitting your
database to heavy traffic this is essential.
There's also an issue with threading and cache. I don't know the status of
this implementation for TurboGears, but I believe you have to disable caching
by hand:
================================================================================
class Issue(SQLObject):
class sqlmeta:
lazyUpdate = True
cacheValues = False
issue = UnicodeCol()
impact = IntCol()
closed = BoolCol(default=False)
project = ForeignKey('Project')
creator = ForeignKey('Person')
creation_date = DateTimeCol(default=datetime.now)
idxProject = DatabaseIndex('project')
idxCreator = DatabaseIndex('creator')
================================================================================
And, finally, one last thing that might be appropriate here is using CASCADE
where it can be used. For example, I can't delete a project that has issues
according to the above declaration. This forces me to go to each and every
issue that belongs to one specific project, delete one by one and then go back
and delete the project itself. If you add a "cascade = True" attribute to
your ForeignKey column, then you'll be able to delete the project and either
your RDBMS server or SQLObject will take care of deleting all issues that
belong to this project. Similarly, using "cascade = False" will prevent
deletion from the other table leaving this one with orphan entries:
================================================================================
class Issue(SQLObject):
class sqlmeta:
lazyUpdate = True
issue = UnicodeCol()
impact = IntCol()
closed = BoolCol(default=False)
project = ForeignKey('Project', cascade = True)
creator = ForeignKey('Person', cascade = False)
creation_date = DateTimeCol(default=datetime.now)
idxProject = DatabaseIndex('project')
idxCreator = DatabaseIndex('creator')
================================================================================
There's still a third option: cascade = "null". This sets the column to NULL
and allows deleting the other column. So, your choice are:
- cascade = True --> Deletes the other record and all records that depends
on it
- cascade = False --> Prevent deleting one record unless there's no other
references to it in your database
- cascade = "null" --> Deletes the other record and set the value of the
respective column in all records that depends on it to NULL
** Note that these are set on the "depending" column not on the "master"
column.
It is a good practice to specify one type of cascading because some database
default to "True" and other to "False". If you explicitly state what you want
then you'll have the same behaviour everywhere.
I hipe I helped a little -- or at least presented people here something new
;-)
Be seeing you,
--
Jorge Godoy <[EMAIL PROTECTED]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---