Django syncdb does not start custom SQL

I'm trying to get my application to run its own SQL query on syncdb with the official method of placing some INSERT statements in /sql/.sql

Now that I have run "manage.py sqlall", all the SQL I want to run is there.

However, after running syncdb, the data I want is not found anywhere in the database! Did I miss something?

EDIT: The application I want to insert inserts into uses southern migrations, and perhaps this is why the original SQL is skipped. Does anyone know how I can get it to run SQL after migration, maybe?

+8
django django-models
source share
2 answers

Sql source data does not start for applications managed by the South. From what I understood, this is by design, although I could not find any formal evidence of this other than this mailing list .

To achieve the same behavior, you can create a wrapper and convert your SQL to Python. So I did this to set the view:

Create and edit hyphenation:

$ python manage.py schemamigration app1 install_foo_view --empty $ vim app1/migrations/*_install_foo_view.py 

Here the migration itself:

 from south.db import db from south.v2 import SchemaMigration class Migration(SchemaMigration): def forwards(self, orm): db.execute("CREATE VIEW app1_foo AS SELECT col1, col2 FROM app1_bar") def backwards(self, orm): db.execute("DROP VIEW app1_foo") # autogenerated models attibute goes here complete_apps = ['app1'] 

To transfer data, the stream looks like:

 $ python manage.py datamigration app1 install_bars $ vim app1/migrations/*_install_bars.py 

Here the migration itself:

 from south.db import db from south.v2 import DataMigration class Migration(DataMigration): def forwards(self, orm): orm.Bar.objects.create(col1='val1', col2='val2') def backwards(self, orm): orm.Bar.objects.filter(col1='val1', col2='val2').delete() # autogenerated models attibute goes here complete_apps = ['app1'] 

The default white paper for data is violated:

  • It does not scale well with respect to circuit changes.

    You must copy and customize the instrument file each time you change the fields of the Bar model. So you get "my_fixture_v1.json", "my_fixture_v2.json", etc.

    Otherwise, if you do not save their version and do not save the latest version, there will be discrepancies between previous versions of the model and the device, and you will not be able to navigate the transitions back and forth.

  • It does not support reverse migration.

The proposed approach takes care of all these problems:

  • Since you use orm.Bar , you have a set of fields that apply to Bar right at this point in history. You will not receive any missing or additional fields.
  • No need to copy anything with Bar changes.
  • You can jump back.

    Note that instead of get().delete() , the combination filter().delete() . Thus, you will not delete the Bar that was changed by the user, and you did not work with Bar.DoesNotExist when you did not find them.

+3
source share

I'm not quite sure what you are doing, but if you want to pre-populate the database, you need to use lights.

https://docs.djangoproject.com/en/1.3/howto/initial-data/

-one
source share

All Articles