我正在使用 python 3.4 在 django 1.8 中编写一个应用程序,我遇到了使用 MySQL 作为数据库后端的问题,这让我完全被难住了。
当我从一个新数据库开始并调用 ./manage.py migrate(或 syncdb)并尝试创建初始数据库时,我得到以下回溯:
(virtualenv)~/projects/projmoj (master ✘)✹✭ ᐅ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: allauth, rest_framework, registration, rest_auth, projmoj, messages, project, staticfiles
Apply all migrations: contenttypes, sites, sessions, task, auth, admin, authtoken, static_precompiler, account
Synchronizing apps without migrations:
Creating tables...
Creating table project_project
Creating table project_membership
Running deferred SQL...
Traceback (most recent call last):
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 220, in execute
self.errorhandler(self, exc, value)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 209, in execute
r = self._query(query)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 371, in _query
rowcount = self._do_query(q)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 335, in _do_query
db.query(q)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1005, 'Can\'t create table `tasks`.`#sql-1c32_9` (errno: 150 "Foreign key constraint is incorrectly formed")')
当我在网络托管服务上执行相同的操作时会发生此错误,它可以工作,但是在托管服务和本地主机上都使用 sqlite。
我没有迁移,这些是我的模型:
class Project(models.Model):
name = models.CharField(max_length = 32)
description = models.TextField(null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
def get_owner(self):
return self.members.filter(status=OWNER).first().user
def get_membership(self, user):
return self.members.filter(user=user).first()
def __str__(self):
return str(self.name) + " - by " + str(self.get_owner().username)
class Membership(models.Model):
user = models.ForeignKey(User, related_name='joined_projects')
project = models.ForeignKey(Project, related_name='members')
status = models.PositiveSmallIntegerField(choices=MEMBER_STATUS)
join_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.user.username)
class Sprint(models.Model):
name = models.CharField(max_length = 32, null=True, blank=True)
start_date = models.DateTimeField()
due_date = models.DateTimeField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='sprints')
status = models.PositiveSmallIntegerField(choices=SPRINT_STATUS, default=PLANNING)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Category(models.Model):
name = models.CharField(max_length = 32)
project = models.ForeignKey(Project, related_name='categories')
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Goal(models.Model):
name = models.CharField(max_length = 32)
description = models.TextField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='goals')
completion = models.PositiveSmallIntegerField(choices=COMPLETION, default=NOT_COMPLETED)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Task(models.Model):
name = models.CharField(max_length = 64)
description = models.TextField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='tasks')
category = models.ForeignKey(Category, related_name='tasks', null=True, blank=True)
goal = models.ForeignKey(Goal, related_name='tasks', null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
sprint = models.ForeignKey(Sprint, related_name='tasks', null=True, blank=True)
status = models.PositiveSmallIntegerField(choices=TASK_STATUS, default=WAITING)
dedicated_hours = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
assigned_worker = models.ForeignKey(User, null=True, blank=True)
priority = models.PositiveSmallIntegerField(choices=PRIORITY, default=NORMAL)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class HourReport(models.Model):
task = models.ForeignKey(Task, related_name='hour_reports')
worker = models.ForeignKey(User, related_name='hour_reports')
hours_spent = models.DecimalField(max_digits=6, decimal_places=2)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.hours_spent) + " on " + str(self.task)
我的数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tasks',
'USER': 'root',
'PASSWORD': 'mysqlmysql',
'HOST': 'localhost',
}
}
版本等:
当谷歌搜索时,这个错误似乎经常出现在人们填充他们的手动 SQL 代码时,但我是通过 django 自动完成的。
有什么想法吗?
最佳答案
日志告诉您您还没有为您的项目 应用程序创建迁移。您应该在运行迁移之前为应用程序创建迁移。
./manage.py makemigrations project
./manage.py migrate
由于它是一个新数据库,您最好在创建和应用迁移之前完全重置它,否则您将不得不手动处理其当前的不一致状态。
关于python - 由于 errno : 150 "Foreign key constraint is incorrectly formed",Django 1.8 应用程序初始迁移神秘失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32573600/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que