我有模型
class SomeModel(models.Model):
target_field = models.ForeignKey(
'OtherModel', on_delete=models.PROTECT, verbose_name=_('Target Field'),
)
Run Code Online (Sandbox Code Playgroud)
我想把它改成
class SomeModel(models.Model):
target_field_id = models.PositiveIntegerField(_('Target field id'))
Run Code Online (Sandbox Code Playgroud)
我想在不丢失由ForeignKey 字段存储在target_field_id 中的数据的情况下执行此操作。
我尝试运行 makemigrations。但它删除列然后创建新列。
非常感谢任何帮助。
我有 django 应用程序,我想将所有迁移从我的项目、身份验证和管理导出到 sql 查询。我能做到
python manage.py sqlmigrate myapp 0001
但是,我想同时导出所有迁移,而不是显式纠正循环。有没有我可以使用的软件包或工具。
使用 ECS Fargate 运行数据库迁移的建议部署策略是什么?
我可以在启动 Gunicorn 服务器之前更新容器命令以运行迁移。但如果配置了多个实例,这可能会导致并发迁移同时执行。
我还必须考虑图像已经在运行的事实。如果我弄清楚如何在新映像启动并运行之前运行迁移,我必须考虑这样一个事实:旧映像仍在旧代码上运行,并且可能会破坏或导致奇怪的数据损坏副作用。
我正在考虑创建一个新的 ECS::TaskDefinition。让其运行一个运行迁移的一次性迁移脚本。然后容器关闭。我更新了所有其他 TaskDefinitions 以使其具有 DependsOn,这样它们在完成之前不会启动。
我正在使用Django 1.7和MySQL.
我有一个现有的模型,我想添加一个BooleanField.我希望该字段默认为true.看起来很简单吧?所以我像这样创建我的字段:
class MyModel(models.Model):
my_field = models.BooleanField(default=True)
Run Code Online (Sandbox Code Playgroud)
然后我运行makemigrations并生成以下迁移:
operations = [
migrations.AddField(
model_name='mymodel',
name='myfield',
field=models.BooleanField(default=True),
preserve_default=True,
),
]
Run Code Online (Sandbox Code Playgroud)
看起来合法,但是当我应用迁移时,在我的数据库中创建的字段没有默认值,它看起来像这样:
my_field tinyint(1) NOT NULL,
现在,当我应用迁移时,现有记录的字段设置为1,但我想要的是迁移创建一个默认为true的字段,或者1.我在这里缺少什么?任何人都可以对我正在做错的事情有所了解吗?非常感谢任何建议!
我有2个django应用程序,即主要和authtools.我跑的时候
python manage.py migrate
,我得到一个CircularDependencyError:
raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.graph.CircularDependencyError: main.0001_initial, authtools.0001_initial
Run Code Online (Sandbox Code Playgroud)
在我的设置文件中,我将AUTH_USER_MODEL定义为:AUTH_USER_MODEL = 'authtools.User'.创建的迁移文件如下所示:对于authtools应用程序,它将依赖关系显示为:
dependencies = [
('main', '__first__'),
('auth', '0001_initial'),
]
Run Code Online (Sandbox Code Playgroud)
而对于主要应用程序,depandancies显示为:
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?
编辑:关于我在问什么似乎有些困惑。该模型用于我在迁移0009中创建的Postgres视图。给人的印象是,如果Django有此managed = False选项,则不会为该模型生成迁移。但是,它仍在尝试创建它。
另外,我将Django 1.8与Python 3.4结合使用。
我在使用以下链接作为指南时,无法为Postgres视图创建Django模型:drdaeman和eceppda的答案是否可以在django中将数据库视图用作模型。我还查找了Django API文档中的Options.managed条目。但是,尽管如此,它仍在创建迁移,该迁移为视图的模型添加了表。
到目前为止,这是我的代码:
class RelevantModel(models.Model):
rebate_pool_total = models.OneToOneField('foo.VirtualTotal', null=True)
total = models.DecimalField(null=True, decimal_places=2, max_digits=32)
class VirtualTotal(models.Model):
relevant_model = models.ForeignKey('foo.RelevantModel')
total = models.DecimalField(null=True, decimal_places=2, max_digits=32)
class Meta:
managed = False
Run Code Online (Sandbox Code Playgroud)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('foo', '0008_previous_migration'),
] …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行以下命令
python manage.py makemigrations
Run Code Online (Sandbox Code Playgroud)
但是,得到错误
ValueError: path is on mount 'C:', start on mount 'F:'
Run Code Online (Sandbox Code Playgroud)
可能是什么原因?
完整的追溯: -
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrations.py", line 189, in handle
self.write_migration_files(changes)
File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrations.py", line 207, in write_migration_files
migration_string = os.path.relpath(writer.path)
File "C:\Python34\lib\ntpath.py", …Run Code Online (Sandbox Code Playgroud) 我在空DB上进行django迁移时遇到问题.什么时候,我想迁移我有一个循环依赖错误.ForeignKeys相关的两个应用程序之间的循环依赖性错误
/firstapp/models.py
class Person(models.Model):
genders_list = (
('m', 'Male'),
('f', 'Female'),
)
user_field = models.OneToOneField(User, on_delete=models.CASCADE)
gender = models.CharField(max_length=1, choices=genders_list, default='m')
phone_1 = models.CharField(max_length=25, verbose_name="Phone number")
phone_2 = models.CharField(max_length=25, verbose_name="Phone number 2", blank=True)
def __str__(self):
return self.user_field.first_name + self.user_field.last_name
class Doctor(Person):
job_position = models.CharField(max_length=255, verbose_name="Job position")
hospital_field = models.ForeignKey('hospital.Hospital', on_delete=models.SET_NULL, null=True, default=None,blank = True)
seat_field = models.ForeignKey('hospital.Seat', on_delete=models.SET_NULL, null=True, default=None,blank = True)
class Patient(Person):
doctor_field = models.ForeignKey('Doctor', on_delete=models.SET_NULL, null=True, default=None)
Run Code Online (Sandbox Code Playgroud)
/secondapp/models.py
class Hospital(models.Model):
name = models.CharField(max_length=255, verbose_name="Name")
description = models.TextField(verbose_name="Description") …Run Code Online (Sandbox Code Playgroud) 当我尝试运行Django migrate命令时,我收到"关系列已存在"错误:
Operations to perform:
Synchronize unmigrated apps: signin, django_rq, gis, staticfiles, admindocs, messages, pipeline, test_without_migrations, django_extensions
Apply all migrations: profile, activities, contenttypes, # plus other modules...
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying activities.0002_auto_20170731_1939...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/srv/http/example.com/venvs/4dc40e5fc12700640a30ae0f040aa07ffc8aa1c5/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/srv/http/example.com/venvs/4dc40e5fc12700640a30ae0f040aa07ffc8aa1c5/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/http/example.com/venvs/4dc40e5fc12700640a30ae0f040aa07ffc8aa1c5/local/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options) …Run Code Online (Sandbox Code Playgroud) 我正在尝试做的是更改django项目中特定应用程序的迁移的默认路径,将其置于项目本身之外,但保持透明,保持使用makemigrations和迁移.可能吗?如果有,怎么样?