使用rake任务重置和重新播种数据库

如何解决使用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:resettask init: [:environment,'db:reset'] 执行db:drop,db:setup,而db:setup执行db:schema:load。并且db:migrate:resetdb:migrate快。

不确定为什么db:schema:load会导致问题。

正如Sergio所建议的db:migrate可能是原因。他在comments db:migrate中的建议将载入所有列。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-