如何解决使用rake任务重置和重新播种数据库
使用下面的代码重置和重新设置数据库
task init: [:environment,'db:migrate:reset','db:seed']
db:seed
创建了几个用户和几个对象。它还会为少数用户更新role
。
db:seed
引发以下错误NoMethodError: undefined method 'role' for User
。
有一个迁移文件,它将role
列添加到User
表中。
无法解决此问题。尝试为db:seed
编写单独的任务。尝试过以下
task init: [:environment,'db:seed'] do
Rake::Task['db:seed'].invoke
end
与以前相同的error
。为什么迁移文件中的column
没有为db:seed
加载,或者我在这里丢失了某些内容?
错误日志:
NoMethodError: undefined method `role' for #<User:0x00007fd5ecb239e0>
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activemodel-6.0.3.2/lib/active_model/attribute_methods.rb:432:in `method_missing'
/Users/Desktop/rails_app/app/models/user.rb:17:in `update_role'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:428:in `block in make_lambda'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:200:in `block (2 levels) in halting'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:605:in `block (2 levels) in default_terminator'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:604:in `catch'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:604:in `block in default_terminator'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:201:in `block in halting'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:513:in `block in invoke_before'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:513:in `each'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:513:in `invoke_before'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:134:in `run_callbacks'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/callbacks.rb:825:in `_run_validation_callbacks'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activemodel-6.0.3.2/lib/active_model/validations/callbacks.rb:117:in `run_validations!'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activemodel-6.0.3.2/lib/active_model/validations.rb:337:in `valid?'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/validations.rb:68:in `valid?'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/validations.rb:84:in `perform_validations'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/validations.rb:53:in `save!'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:318:in `block in save!'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:212:in `transaction'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/transactions.rb:318:in `save!'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/suppressor.rb:48:in `save!'
/Users/Desktop/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/persistence.rb:55:in `create!'
/Users/Desktop/rails_app/app/services/init_data:6:in `process'
/Users/Desktop/rails_app/db/seeds.rb:3:in `<main>'
解决方法
使用 Select * from [Frs_def_businessobjectlayouts] where Definition like '%Open In Parent%' AND name like
'Task.ResponsiveAnalyst'
解决了该问题。即
#new
import turtle
import random
import time
import winsound
import tkinter
import os
delay = 0.1
#score
score = 0
lifes = 5
#pause
p = turtle.Turtle()
p.speed(0)
p.shape('square')
p.color('black')
p.penup()
p.ht()
p.goto(200,270)
p.write('Press p to pause .',align='center',font=('courier',15,'bold'))
#lifes
life = turtle.Turtle()
life.speed(0)
life.shape('square')
life.color('blue')
life.penup()
life.ht()
life.goto(-240,270)
life.write('Lives: 5','bold'))
#hole
hole = turtle.Turtle()
hole.speed(0)
hole.color('orange')
hole.penup()
#screen
wn = turtle.Screen()
wn.title('Game')
wn.bgcolor('orange')
wn.setup(width = 600,height = 600)
wn.tracer(0)
#restart
def restart():
exec(open("C:/Users/liamh/Desktop/newone.py ").read())
turtle.Screen().clear()
wn.onkeypress(restart,'r')
#score
pen = turtle.Turtle()
pen.speed(0)
pen.shape('square')
pen.color('green')
pen.penup()
pen.ht()
pen.goto(0,270)
pen.write('Score: 0',21,'bold'))
#sprite1
sprite1 = turtle.Turtle()
sprite1.speed(0)
sprite1.shape('triangle')
sprite1.color('red')
sprite1.penup()
sprite1.goto(10,0)
sprite1.direction = 'stop'
#boundries
mypen = turtle.Turtle()
mypen.penup()
mypen.setposition(-280,-280)
mypen.pendown()
mypen.pensize(3)
for side in range(4):
mypen.forward(550)
mypen.left(90)
mypen.hideturtle()
#food
s = turtle.Turtle()
s.speed(0)
s.shape('square')
s.color('red')
s.penup()
s.goto(0,100)
#functions
speed = 1
def turnr():
sprite1.right(30)
def turnl():
sprite1.left(30)
wn.onkeypress(turnr,'Right')
wn.onkeypress(turnl,'Left')
wn.update()
#game ending/timer
paused = False
def toggle_pause():
global paused
if paused == True:
paused = False
else:
paused = True
wn.listen()
wn.onkeypress(toggle_pause,'p')
#MAIN LOOP
while True:
if not paused:
sprite1.forward(speed)
wn.update()
else:
wn.update()
#boundrary
if sprite1.xcor()> 270 or sprite1.xcor()< -270:
winsound.PlaySound('C:/Users/liamh/Downloads/smb_bump.wav',winsound.SND_ASYNC)
sprite1.right(180)
delay -= 0.01
lifes -= 1
life.clear()
life.write('Lives: {}'.format(lifes),'bold'))
elif sprite1.ycor()> 270 or sprite1.ycor()< -270:
winsound.PlaySound('C:/Users/liamh/Downloads/smb_bump.wav',winsound.SND_ASYNC)
sprite1.left(180)
delay -= 0.01
lifes -= 1
life.clear()
life.write('Lives: {}'.format(lifes),'bold'))
#check collision
if sprite1.distance(s) < 20:
speed += 0.05
#move random
winsound.PlaySound('C:/Users/liamh/Downloads/smb_coin.wav',winsound.SND_ASYNC)
x = random.randint(-250,250)
y = random.randint(-250,250)
s.goto(x,y)
delay -= 0.001
score += 1
pen.clear()
pen.write('Score: {}' .format(score),'bold'))
wn.update()
elif lifes == 0:
#end game
wn.update()
end = turtle.Turtle()
end.speed(0)
end.shape('square')
end.color('black')
end.penup()
end.ht()
end.goto(0,0)
end.write('GAME OVER! Score: {}' .format(score),24,'bold'))
sprite1.goto (0,60)
speed = 0
wn.update()
#restart
def restart():
wn.clear()
exec(open("C:/Users/liamh/Desktop/newone.py ").read())
wn.onkeypress(restart,'r')
re = turtle.Turtle()
re.speed(0)
re.shape('square')
re.color('black')
re.penup()
re.ht()
re.goto(0,-25)
re.write('Press r to restart','bold'))
wn.mainloop()
db:reset执行db:reset
。
task init: [:environment,'db:reset']
执行db:drop,db:setup
,而db:setup
执行db:schema:load
。并且db:migrate:reset
比db:migrate
快。
不确定为什么db:schema:load
会导致问题。
正如Sergio所建议的db:migrate
可能是原因。他在comments db:migrate
中的建议将载入所有列。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。