Model Meta option: disable SQL generation
|Reported by:||honeyman||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||0.96|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
A new Meta option would be nice to have for models. It could selectively disable generation of SQL (related to the model) in operations like sqlreset/sqlall/etc.
Proposed option name is sql_generation_required; default value (no legacy behaviour change) is True. If sql_generation_required is False for the model, this means that the SQL generation on sqlreset/sqlall/syncdb/etc does not perform any DROP TABLE/ALTER TABLE/CREATE TABLE/CREATE INDEX for this model. Note that sqlcustom still generates the code related to this model (if someone wants to block the custom datafill from getting into the sqlall file, one should remove the custom sql datafill rather than set any meta options).
Real life use case is: django application which heavily mixes the tables generated automatically by Django ORM and the custom SQL code - custom indexes, custom constraints, custom views. If we want to create an SQL view and use it in Django, the most obvious way is to create the SQL view manually (CREATE VIEW myview AS ...), and then generate the Django model resembling this view (class MyView(models.Model): class Meta: db_table = 'myview'). But this will mean, that any attempt to regenerate the django-controlled tables (manage.py reset mysite) will cause an attempt to drop this model like it was a table (DROP TABLE myview;) and recreate it again (CREATE TABLE myview (...) ) - and both actions will fail.
The most simple way to fix this is to mark only the custom-controlled model ("myview" in our case) in the way so that "manage.py reset mysite" will not touch it at all. This could be done in the following way:
db_table = 'myview'
sql_generation_required = False
Below goes the proposed patch for this feature; sorry for GNU diff format and for being based on 0.96 rather than trunk - no SVN here.