Rails建模:通过以下方式将HABTM转换为has_many: [英] Rails modeling: converting HABTM to has_many :through

查看:105
本文介绍了Rails建模:通过以下方式将HABTM转换为has_many:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在现有的Rails站点上进行维护工作,并且由于多对多的关联而遇到了一些问题.看起来该站点最初是使用has_and_belongs_to_many来建立的,一些关系在业务逻辑中变得更加复杂,因此我需要使用has_many :through来支持关系表中的其他字段.但是,最初用于HABTM的联接表没有主键,我必须添加一个主键以支持使用has_many :through进行单独的关系建模.

I'm doing maintenance work on an existing Rails site and am having some problems stemming from many-to-many associations. It looks like the site was initially built using has_and_belongs_to_many for a few relationships that have since gotten more complicated in the business logic, so I need to use has_many :through instead to support additional fields in the relationship table. However, the join table that was initially used for HABTM doesn't have a primary key, and I've got to add one to support separate relationship modeling using has_many :through.

将主键添加到具有大量数据的现有表中的最佳方法是什么?还有另一种方法可以做我想做的事吗?

What's the best way to add a primary key to an existing table with lots of data? Is there another way to do what I'm trying to?

偶然地,系统在Oracle上运行.

Incidentally, the system is running on Oracle.

谢谢!

Justin

UPDATE 11/9/09 3:58 pm:我不是Oracle专家,并且迷失在Oracle版本的非null,自动递增等版本中.最初,我尝试通过添加新字段作为主键来执行Mike和Corey的建议,但是Oracle不允许我将非空字段添加到非空表(ORA-01758).然后,我将数据导出为SQL,删除行,添加PK并将其设置为非空,然后尝试导入数据,但是我不断遇到无法将NULL插入ID ..."的错误提示. (ORA-01400).

UPDATE 11/9/09 3:58pm: I'm not an Oracle expert and have been getting lost in the wilds of Oracle's versions of not null, auto-increment, and so forth. Initially I tried doing what Mike and Corey recommended by adding a new field as a primary key, but Oracle wouldn't let me add a non-null field to a non-empty table (ORA-01758). I then exported the data as SQL, dropped the rows, added the PK and set it to be non-null, then tried to import the data, but I kept getting errors to the tune of "cannot insert NULL into id..." (ORA-01400).

最后,我尝试使用Corey在他的评论中建议的迁移,但是rake遇到了与我手动更改数据库时Oracle抛出的错误相同的错误(无法将非null字段添加到非空表").我清除了表,进行了迁移(有效),然后尝试重新导入数据,但是上次尝试导入时出现了相同的错误(无法将NULL插入ID ...").如何保存数据并添加所需的主键?我知道有人建议编写瑞克任务,但我不确定如何在这方面进行.有什么想法吗?

Finally, I tried using a migration as Corey suggests in his comment, but rake hit the same errors that Oracle was throwing when I altered the database manually ("cannot add non-null field to non-empty table"). I cleared the table, ran the migration (which worked), and then attempted to re-import the data, but I got the same errors last time I'd tried to import ("cannot insert NULL into id..."). How can I save my data and add the primary keys I need? I know that the possibility of writing a rake task was suggested, but I'm unsure as to how to proceed on that front. Any ideas?

推荐答案

您需要创建新列,用PK值填充它,然后在新列上创建PK,例如:

You need to create the new column, fill it with the PK values and then create a PK on the new column, eg:

SQL> create table no_pk_tab (c1 varchar2(10), c2 varchar2(10))
Table created.
SQL> insert into no_pk_tab values ('one', 'one')
1 row created.
SQL> insert into no_pk_tab values ('two', 'two')
1 row created.

SQL> alter table no_pk_tab add (id integer)
Table altered.

SQL> create sequence no_pk_seq
start with 1
increment by 1
Sequence created.

SQL> update no_pk_tab set id = no_pk_seq.nextval
2 rows updated.
SQL> select * from no_pk

C1         C2             PK_COL
---------- ---------- ----------
one        one                 1
two        two                 2

2 rows selected.

SQL> alter table no_pk add primary key (pk_col) using index
Table altered.

根据表中的行数,填充序列值可能要花一些时间,但是它可以工作.

Depending on how many rows are in your table it may take a while to populate the sequence values, but it will work.

这篇关于Rails建模:通过以下方式将HABTM转换为has_many:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆