Ruby/Rails

编程入门 行业动态 更新时间:2024-10-26 18:21:30
本文介绍了Ruby/Rails - Active Record Db 迁移到 MySQL - 时间戳类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Redmine,当我运行 rake db 迁移时,数据库会在 mysql 中创建.我的问题是日期字段错误.我希望它是 mysql 时间戳类型,但它是 MySQL 中的 DATETIME 类型.

class CreateChats

此外,如果我对此迁移进行了更改,我该如何让它重新制作表格(删除它不起作用)?

解决方案

如果需要,您始终可以插入具有自定义类型的列.符号名称会自动转换为 ActiveRecord 定义的任何内容,但如果您使用纯字符串,则按原样输入:

t.column :ar_timestamp, :timestampt.column :mysql_timestamp, '时间戳'

你得到的是 ar_timestamp 是通常的 DATETIME 类型,其中 mysql_timestamp 被定义为 TIMESTAMP.

I am using Redmine and when I run a rake db migration the database gets created in mysql ok. My problem is that the date field is wrong. I want it to be a mysql timestamp type, but instead it is a DATETIME type in MySQL.

class CreateChats < ActiveRecord::Migration def self.up create_table :chats do |t| t.column :message, :string t.column :user, :integer t.column :sendDate, :timestamp end end def self.down drop_table :chats end end

In addition if I make a change to this migration how do I get it to remake the table (deleting it doesn't work)?

解决方案

You can always insert a column with a custom type if you want. The symbol names are automatically converted into whatever ActiveRecord defines, but if you use a plain string it goes in as-is:

t.column :ar_timestamp, :timestamp t.column :mysql_timestamp, 'timestamp'

What you get is ar_timestamp being the usual DATETIME type where mysql_timestamp is defined as TIMESTAMP.

更多推荐

Ruby/Rails

本文发布于:2023-10-16 10:41:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497338.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ruby   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!