Ruby on Rails+PostgreSQL:自定义序列的使用 [英] Ruby on Rails+PostgreSQL: usage of custom sequences
问题描述
假设我有一个名为 Transaction
的模型,它有一个 :transaction_code
属性.我希望该属性自动填充可能不同于 id
的序列号(例如,具有 id=1
的事务可能具有 transaction_code=1000
).
Say I have a model called Transaction
which has a :transaction_code
attribute.
I want that attribute to be automatically filled with a sequence number which may differ from id
(e.g. Transaction with id=1
could have transaction_code=1000
).
我尝试在 postgres 上创建一个序列,然后将 transaction_code
列的默认值设为该序列的 nextval
.问题是,如果我没有为 RoR 上的 @transaction.transaction_code
分配任何值,当我在 RoR 上发出 @transaction.save
时,它会尝试执行以下操作SQL:
I have tried to create a sequence on postgres and then making the default value for the transaction_code
column the nextval
of that sequence.
The thing is, if I do not assign any value to @transaction.transaction_code
on RoR, when I issue a @transaction.save
on RoR, it tries to do the following SQL:
INSERT INTO transactions (transaction_code) VALUES (NULL);
这样做是在 Transactions 表上创建一个新行,其中 transaction_code 为 NULL,而不是计算序列的 nextval 并将其插入相应的列.因此,正如我发现的那样,如果您为 postgres 指定 NULL,它假定您真的想将 NULL 插入该列,而不管它是否具有默认值(我来自具有不同行为的 ORACLE).
What this does is create a new row on the Transactions table, with transaction_code as NULL, instead of calculating the nextval of the sequence and inserting it on the corresponding column. Thus, as I found out, if you specify NULL to postgres, it assumes you really want to insert NULL into that column, regardless of it having a default value (I'm coming from ORACLE which has a different behavior).
我愿意接受任何解决方案,无论是在数据库上还是在 RoR 上完成:
I'm open to any solution on this, either if it is done on the database or on RoR:
- 有一种方法可以从 ActiveRecord 中排除属性
保存
- 或者有一种方法可以在使用触发器插入之前更改列的值
- 或者有一种方法可以在 RoR 中生成这些序列号
- 或任何其他方式,只要它有效 :-)
提前致谢.
推荐答案
目前,您可能会像这样在 ROR 模型中获取和分配序列:
For the moment, you might be stuck fetching and assigning the sequence in your ROR model like this:
before_create :set_transaction_code_sequence
def set_transaction_code_sequence
self.transaction_code = self.class.connection.select_value("SELECT nextval('transaction_code_seq')")
end
我不是特别喜欢这个解决方案,因为我想直接在 AR 中看到这个解决方案......但它确实可以解决问题.
I'm not particularily fond of this solution, since I'd like to see this corrected in AR directly... but it does do the trick.
这篇关于Ruby on Rails+PostgreSQL:自定义序列的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!