在rails中创建带有数据库迁移的集合列
我需要向数据库中的用户表添加一个新列。我想要设置列的类型。该列代表用户的性别。该集合应该有两个选项。一种代表男性“m”,另一种代表女性“f”。
但我还没有找到任何用于添加具有设置类型的列的文档。
我该怎么做?
I need to add a new column to my users table in the database. I want the type of the column to be set. The column represents the users gender. There should be two options to the set. One form Male "m" and the other for Female "f".
But I haven't found any documentation for adding a column with the set type.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用什么数据库? mysql?如果你想使用 SET 数据类型,你必须手动完成,因为 Rails 不支持它。然而,我这样做只是
为了方便。
What db is used? mysql? If you're want to use SET datatype, you'll have to do it manually, as rails doesn't support it. However, I'd do just
for the sake of convenience.
在您的用户模型中,您应该添加以下行来要求 M/F 答案。
In your Users model, you should add the following line to require M/F answers.
我认为你想添加带有默认数据类型的性别列(如果我错了,请纠正我),如果是这样,这里会有一个步骤,
我假设“M”代表男性,“F”代表女性(你如果您愿意,也可以使用整数)
创建迁移
这将为您创建一个迁移,顾名思义,它将
在您的迁移 self.up 操作中
的用户表中添加一个性别列,在这里添加它,它表示我们正在添加性别字符串类型的列,其默认值为“m”,
并将其添加到 self.down 事件中
,这样您的最终迁移将看起来像这样
,就
这样,希望这有帮助
I think you want to add the gender column with a default datatype (correct me if I'm wrong), If so there would be the step
here I'm assuming 'M' is for male and "F" is for female (you can use integers also if you wish)
create a migration
This will create a migration for you and as the name implies it will add a gender column to your users table
in your migrations self.up action add this
here it says we are adding a gender column of string type and its default values is 'm'
and add this to self.down events
so your final migration will look something like this
and do a
thats it, hope this helps