将Django模型迁移到Postgresql模式

时间:2019-02-18 16:34:47

标签: django postgresql database-schema

我想从Django迁移中的特定Postgresql模式(即“ schema1”)中创建新表。

尽管遵循this blogthis post的方法1,迁移仍将表发送到默认模式“ public”而不是“ schema1”。

+ aws_instance.zookeeper[0] id: ami: "ami-09693313102a30b2c" arn: associate_public_ip_address: "false" availability_zone: cpu_core_count: cpu_threads_per_core: credit_specification.#: "1" credit_specification.0.cpu_credits: "unlimited" disable_api_termination: "true" ebs_block_device.#: ebs_optimized: "false" ephemeral_block_device.#: get_password_data: "false" host_id: iam_instance_profile: "devl-ZOOKEEPER_IAM_PROFILE" instance_state: instance_type: "t3.small" ipv6_address_count: ipv6_addresses.#: key_name: "devl-key" monitoring: "false" network_interface.#: network_interface_id: password_data: placement_group: primary_network_interface_id: private_dns: private_ip: public_dns: public_ip: root_block_device.#: "1" root_block_device.0.delete_on_termination: "true" root_block_device.0.volume_id: root_block_device.0.volume_size: "16" root_block_device.0.volume_type: "gp2" security_groups.#: source_dest_check: "false" subnet_id: "subnet-5b8d8200" tags.%: "3" tags.Description: "Do not terminate more than one at a time" tags.Env: "devl" tags.Name: "devl-zookeeper-0" tenancy: user_data: "70fd2ae9f7da42e2fb15328cd6539c4f7ed4a5be" volume_tags.%: vpc_security_group_ids.#: "1" vpc_security_group_ids.3423986071: "sg-03911aa28dbcb3f20" -/+ aws_lb_target_group_attachment.SchemaRegistryTgAttach[0] (new resource required) id: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034-20190218210336558900000001" => (forces new resource) port: "8081" => "8081" target_group_arn: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" => "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" target_id: "i-03ed28ab175c0f684" => "${element(aws_volume_attachment.kafka_att.*.instance_id,count.index)}" (forces new resource) -/+ aws_lb_target_group_attachment.SchemaRegistryTgAttach[1] (new resource required) id: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034-20190218210336576900000002" => (forces new resource) port: "8081" => "8081" target_group_arn: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" => "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" target_id: "i-0b39bd7244f32809f" => "${element(aws_volume_attachment.kafka_att.*.instance_id,count.index)}" (forces new resource) -/+ aws_lb_target_group_attachment.SchemaRegistryTgAttach[2] (new resource required) id: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034-20190218210336671000000003" => (forces new resource) port: "8081" => "8081" target_group_arn: "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" => "arn:aws:elasticloadbalancing:eu-west-1:544611607123:targetgroup/devl-KafkaSchemaRegistryTG/46193714a87ea034" target_id: "i-0bbd8d3a10890b94c" => "${element(aws_volume_attachment.kafka_att.*.instance_id,count.index)}" (forces new resource) + aws_volume_attachment.kafka_att[0] id: device_name: "/dev/sdh" instance_id: "${element(aws_instance.zookeeper.*.id,count.index)}" volume_id: "vol-021d1530117f31905" 中,我有:

settings.py

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'OPTIONS': { 'options': '-c search_path=django,public' }, 'NAME': 'myDB', 'USER': 'username', 'PASSWORD': '***', 'HOST': 'my.host.address', 'PORT': '1234', }, 'schema1': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'OPTIONS': { 'options': '-c search_path=schema1,public' }, 'NAME': 'myDB', 'USER': 'username', 'PASSWORD': '***', 'HOST': 'my.host.address', 'PORT': '1234', } } #Path to DBrouter to handle PG schemas https://stackoverflow.com/a/51007441/3976696 DATABASE_ROUTERS = ('djangogirls.dbrouters.MyDBRouter',) 中,我有:

djangogirls/dbrouters.py

还有我要迁移的模型类,位于from legacydbapp.models import MyUser # Include here any class (i.e. table) that belongs to the schema "schema1" ROUTED_MODELS_SCHEMA1 = [MyUser] class MyDBRouter(object): """ A router to place DB queries into correct schema depending on considered tables. """ def db_for_read(self, model, **hints): if model in ROUTED_MODELS_SCHEMA1 : return 'schema1' return None def db_for_write(self, model, **hints): if model in ROUTED_MODELS_SCHEMA1 : return 'schema1' return None

models.py

我运行了以下命令:

class MyUser(models.Model):
    first_name = models.CharField(max_length=30, default='',null=True, blank=True) 
    last_name = models.CharField(max_length=30, default='', null=True, blank=True)
    profession = models.CharField(max_length=32,default='', null=True, blank=True)
    def __str__(self):
        return self.first_name + " " + self.last_name
    class Meta:
        managed = True
        db_table = 'myuser'

然后sqlmigrate返回以下SQL:

$ python manage.py makemigrations legacydbapp
$ python manage.py sqlmigrate legacydbapp 0001_initial
$ python manage.py migrate legacydbapp

如果DB路由器正在工作,我希望SQL改为读取BEGIN; -- -- Create model MyUser -- CREATE TABLE "myuser" ( "id" serial NOT NULL PRIMARY KEY, "first_name" varchar(30) NULL, "last_name" varchar(30) NULL, "profession" varchar(32) NULL); COMMIT; ,但事实并非如此。我是不是在某个地方搞砸了,还是在Django 2.1.5中根本无法实现?

1 个答案:

答案 0 :(得分:1)

运行迁移时,必须显式提供数据库定义的名称:

$ python manage.py migrate legacydbapp --database schema1

为确保仅在特定数据库中创建模型MyUser,您的路由器必须实现.allow_migrate()