from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
class Migration(migrations.Migration):
initial = True
dependencies = [ ]
operations = [
migrations.CreateModel(
name='SampleModel1',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
],
),
]
$ ./manage.py sqlmigrate app1 0001
BEGIN;
--
-- Create model SampleModel1
--
CREATE TABLE `app1_samplemodel1` (`id` integer
AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(200) NOT
NULL);
COMMIT;
$ ./manage.py migrate
Operations to perform:
Apply all migrations: admin, app1, auth,
contenttypes, sessions
Running migrations:
Applying app1.0001_initial... OK
id app name applied
12 auth
0008_alter_user_usernam
e_max_length
2017-03-31 05:02:26.307972
13 sessions 0001_initial 2017-03-31 05:02:26.361356
14 app1 0001_initial 2017-03-31 05:29:31.189362
$ python manage.py reset_db
You have requested a database reset.
This will IRREVERSIBLY DESTROY
ALL data in the database "migrations_demo".
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Reset successful.
$ python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
app1
[X] 0001_initial
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
djangoのmigrationはどう動いているか
djangoのmigrationはどう動いているか

djangoのmigrationはどう動いているか

  • 18.
    from django.db importmodels class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
  • 19.
    CREATE TABLE myapp_person( "id" serial NOT NULL PRIMARY KEY, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL );
  • 30.
    class Migration(migrations.Migration): initial =True dependencies = [ ] operations = [ migrations.CreateModel( name='SampleModel1', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=200)), ], ), ]
  • 31.
    $ ./manage.py sqlmigrateapp1 0001 BEGIN; -- -- Create model SampleModel1 -- CREATE TABLE `app1_samplemodel1` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(200) NOT NULL); COMMIT;
  • 35.
    $ ./manage.py migrate Operationsto perform: Apply all migrations: admin, app1, auth, contenttypes, sessions Running migrations: Applying app1.0001_initial... OK
  • 36.
    id app nameapplied 12 auth 0008_alter_user_usernam e_max_length 2017-03-31 05:02:26.307972 13 sessions 0001_initial 2017-03-31 05:02:26.361356 14 app1 0001_initial 2017-03-31 05:29:31.189362
  • 45.
    $ python manage.pyreset_db You have requested a database reset. This will IRREVERSIBLY DESTROY ALL data in the database "migrations_demo". Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes Reset successful.
  • 48.
    $ python manage.pyshowmigrations admin [X] 0001_initial [X] 0002_logentry_remove_auto_add app1 [X] 0001_initial auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages [X] 0008_alter_user_username_max_length contenttypes [X] 0001_initial [X] 0002_remove_content_type_name sessions [X] 0001_initial