[英]Ruby on Rails : how to generate an application-wide sequence?
I am developing on RoR 4, with Oracle, PostGreSQL and MSSQL as target databases. 我正在开发RoR 4,Oracle,PostGreSQL和MSSQL作为目标数据库。
I am building a hierarchy of 4 objects, for which I need to display parent-child relationships through the same query whatever level I start from. 我正在构建一个包含4个对象的层次结构,我需要通过相同的查询显示父子关系。 Not easy to figure out, but the hint is that none of the object should have identical IDs .
不容易弄清楚,但提示是没有任何对象应该具有相同的ID 。
The issue here is that rails maintains a dedicated sequence for each object, so duplicated IDs will appear for sure. 这里的问题是rails为每个对象维护一个专用序列,因此肯定会出现重复的ID。
How can I create a sequence to fill a unique_id field which remains unique for all my data ? 如何创建一个序列来填充unique_id字段,该字段对于我的所有数据都是唯一的?
Thanks for your help, 谢谢你的帮助,
Best regards, 最好的祝福,
Fred 弗雷德
I finally found this solution: 我终于找到了这个解决方案
1 - create a sequence to be used by each of concerned objects 1 - 创建每个相关对象使用的序列
class CreateGlobalSequence < ActiveRecord::Migration
def change
execute "CREATE SEQUENCE global_seq INCREMENT BY 1 START WITH 1000"
end
end
2 - Declare this sequence to be used for identity columns in each of concerned models 2 - 声明此序列用于每个相关模型中的标识列
class BusinessProcess < ActiveRecord::Base
self.sequence_name = "global_seq"
...
end
class BusinessRule < ActiveRecord::Base
self.sequence_name = "global_seq"
...
end
and so on. 等等。 It works fine.
它工作正常。
Rails is great ! Rails太棒了!
Thanks for your help, and best regards, 感谢您的帮助和最诚挚的问候,
Fred 弗雷德
Id column for each table is unique identifier for each table record. 每个表的Id列是每个表记录的唯一标识符。 It will not make any impact on other table Id column.
它不会对其他表Id列产生任何影响。
Don't know why you need this. 不知道你为什么需要这个。 But you can achieve it by some extent.
但是你可以在某种程度上实现它。 Like below :
如下所示:
class CreateSimpleModels < ActiveRecord::Migration
def self.up
create_table :simple_models do |t|
t.string :xyz
t.integer :unique_id
t.timestamps
end
execute "CREATE SEQUENCE simple_models_unique_id_seq OWNED BY
simple_models.unique_id INCREMENT BY 1 START WITH 100000"
end
def self.down
drop_table :simple_models
execute "DELETE SEQUENCE simple_models_unique_id_seq"
end
end
But after 100000 record in db it will again going to similar for other model. 但是在db中100000记录后,它将再次与其他型号相似。
The default id column has the identity
attribute, which is stored per-table. 默认id列具有
identity
属性,该属性存储在每个表中。 If your models fit the bill for Single Table Inheritance you'd be able to define a custom id attribute on the base class. 如果您的模型符合单表继承的帐单,您就可以在基类上定义自定义ID属性。 In your case since you said it's a hierarchy that might be the way to go.
在你的情况下,因为你说它是一个层次结构,可能是要走的路。
The harder? 更难? (STI is a bit to digest but very powerful) way of doing this involves what I'm working on this similar issue with a shared PAN (Private Account Number in this system) in a shared namespace.
(STI有点消化,但非常强大)这样做的方式涉及我正在使用共享命名空间中的共享PAN(此系统中的专用帐号)处理此类似问题。
class CreatePans < ActiveRecord::Migration
def change
create_table :pans do |t|
t.string :PAN
t.timestamps
end
end
end
class AddPanIdToCustomers < ActiveRecord::Migration
def change
add_column :customers, :pan_id, :integer
end
end
The first migration will add the ID table, the second adds the foreign key to the customers table. 第一次迁移将添加ID表,第二次将外键添加到customers表。 You'll also need to add the relationships to the models
has_many :pans
and belongs_to :customers
. 您还需要将关系添加到模型
has_many :pans
和belongs_to :customers
。 You can then refer to their identity by the :pan_id
attribute (however you name it). 然后,您可以通过
:pan_id
属性引用它们的标识(但是您可以将其命名)。 It's a roundabout way of doing things, but in my case business requirements force it - hacky as it is. 这是一种迂回的做事方式,但就我而言,业务要求迫使它 - 实际上是hacky。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.