带有ActiveRecord和Postgresql的枚举类型

编程入门 行业动态 更新时间:2024-10-25 14:21:57
本文介绍了带有ActiveRecord和Postgresql的枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从SitePoint跟踪教程到将模型属性设置为Enum值,Rails从4.1开始支持该值。

我尝试添加一个Season Enum而不是Gender Enum。

>

这是我在 schema.db

中遇到的问题

#由于遵循StandardError #而无法转储表学期#列季节 的未知类型季节 pre>

这是我的迁移:

class AddSeasonToSemesters< ActiveRecord :: Migration [5.1] def up 执行<-SQL 创建类型季节,例如ENUM( fall, winter, spring, summer) ; SQL add_column:semesters,:season,:season,index:true end def down remove_column:semesters,:季节 执行<-SQL DROP TYPE季节; SQL 结束结束

我的 model 文件:

class Semester< ApplicationRecord 枚举季节:{秋季:'fall',冬季:'winter',春季:'spring',夏季:'summer'} 结尾

任何人都知道我在做什么错吗?任何方向将不胜感激,谢谢。

解决方案

您需要从 db / schema.rb切换到到 db / structure.sql 。

潜在的问题是 schema.rb 是ActiveRecord看到的数据库结构的表示形式,但是ActiveRecord不了解很多东西(例如 create type ,CHECK约束以及其他在PostgreSQL中执行的中执行some_raw_sql 语句的操作)。您可以全部<创建类型,但 schema.rb 永远看不到。

如果要使用ActiveRecord无法理解的功能,则必须使用 db / structure.sql 来存储数据库的结构。 structure.sql 以数据库理解的方式存储数据库的结构,而不是ActiveRecord理解的。

切换很容易:

  • 更新您的 config / application.rb 以包含 config.active_record.schema_format =:sql 。
  • 对 rake db:structure:dump 进行操作得到一个初始的 db / structure.sql 。
  • 删除 db / schema.rb 从目录树和版本控制中。
  • 将 db / structure.sql 添加到版本控制中。
  • 调整耙的习惯:
    • 使用 db:structure:dump 代替 db:schema:dump
    • 使用 db:structure:load 代替 db:schema:load
  • 也就是说,我不确定PostgreSQL的本机 enum 类型与ActiveRecord交互的程度如何,因为我从未做过。 AR的枚举 s 是字符串和整数之间的客户端翻译,但 PostgreSQL的枚举 s 是在数据库内部处理的,彼此之间并不了解。可能会有冲突,您需要确保彼此保持同步。

    I am following this tutorial from SitePoint to set a model property to an Enum value, which is supported by Rails as of 4.1.

    Instead of Gender Enum, I am trying to add a Season Enum.

    This is the issue I get in my schema.db

    # Could not dump table "semesters" because of following StandardError # Unknown type 'season' for column 'season'

    This is my migration:

    class AddSeasonToSemesters < ActiveRecord::Migration[5.1] def up execute <<-SQL CREATE TYPE season AS ENUM ('fall', 'winter', 'spring', 'summer'); SQL add_column :semesters, :season, :season, index: true end def down remove_column :semesters, :season execute <<-SQL DROP TYPE season; SQL end end

    And my model file:

    class Semester < ApplicationRecord enum season: { fall: 'fall', winter: 'winter', spring: 'spring', summer: 'summer' } end

    Any idea what I am doing wrong? Any direction would be appreciated, thank you.

    解决方案

    You need to switch from db/schema.rb to db/structure.sql.

    The underlying problem is that schema.rb is a representation of the database's structure as ActiveRecord sees it but ActiveRecord doesn't understand a lot of things (such as create type, CHECK constraints, and other things that show up in execute some_raw_sql statements in migrations) that PostgreSQL does. You can create type all you want but schema.rb will never see it.

    If you want to use things that ActiveRecord doesn't understand then you have to use db/structure.sql to store your database's structure. structure.sql stores the database's structure as the database understands it, not as ActiveRecord understands it.

    Switching is easy:

  • Update your config/application.rb to contain config.active_record.schema_format = :sql.
  • Do a rake db:structure:dump to get an initial db/structure.sql.
  • Delete db/schema.rb from your directory tree and revision control.
  • Add db/structure.sql to revision control.
  • Adjust your rake habits:
    • Use db:structure:dump instead of db:schema:dump
    • Use db:structure:load instead of db:schema:load
  • That said, I'm not sure how well PostgreSQL's native enum types will interact with ActiveRecord as I've never done it. AR's enums are a client-side translation between strings and integers but PostgreSQL's enums are handled inside the database and they don't know about each other. There might be conflicts and you will need to be sure to keep them synchronized with each other.

    更多推荐

    带有ActiveRecord和Postgresql的枚举类型

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

    发布评论

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

    >www.elefans.com

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