新分配的顺序是不工作 [英] Newly assigned Sequence is not working

查看:98
本文介绍了新分配的顺序是不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PostgreSQL中,我创建了一个新表,并分配一个新的序列到 ID 列。如果我在PostgreSQL控制台插入一条记录它的工作原理,但是当我试图从Rails的进口记录,它抛出一个异常,它无法找到相关序列。

In PostgreSQL, I created a new table and assigned a new sequence to the id column. If I insert a record from the PostgreSQL console it works but when I try to import a record from from Rails, it raises an exception that it is unable to find the associated sequence.

下面的表格:

\d+ user_messages;
                                                  Table "public.user_messages"
   Column    |            Type             |                         Modifiers                          | Storage  | Description 
-------------+-----------------------------+------------------------------------------------------------+----------+-------------
 id          | integer                     | not null default nextval('new_user_messages_id'::regclass) | plain    | 

但是,当我试图让与Rails使用SQL查询的顺序,返回NULL:

But when I try to get the sequence with the SQL query which Rails uses, it returns NULL:

select pg_catalog.pg_get_serial_sequence('user_messages', 'id');
 pg_get_serial_sequence 
------------------------

(1 row)

被提出Rails的错误是:

The error being raised by Rails is:

UserMessage.import [UserMessage.new]
NoMethodError: undefined method `split' for nil:NilClass
    from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:910:in `default_sequence_name'

此问题只发生在我使用ActiveRecord的扩展,进口散装记录单记录打通ActiveRecord的保存。

This problem only occurs when I use the ActiveRecord extension for importing bulk records, single records get saved through ActiveRecord.

如何解决呢?

推荐答案

我觉得你的问题是,你设置这一切了手,而不是使用系列列。当您使用系列列,PostgreSQL将创建序列,建立了相应的默认值,并确保序列是由有问题的表和列拥有。从<一个href="http://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-INFO-CATALOG-TABLE"相对=nofollow>精细的手工:

I think your problem is that you set all this up by hand rather than by using a serial column. When you use a serial column, PostgreSQL will create the sequence, set up the appropriate default value, and ensure that the sequence is owned by the table and column in question. From the fine manual:

pg_get_serial_sequence(TABLE_NAME,列名)
  得到一个串行或BIGSERIAL列使用了

pg_get_serial_sequence(table_name, column_name)
get name of the sequence that a serial or bigserial column uses

但你不使用系列 BIGSERIAL pg_get_serial_sequence 也无济于事。

But you're not using serial or bigserial so pg_get_serial_sequence won't help.

您可以通过做解决这个问题:

You can remedy this by doing:

alter sequence new_user_messages_id owned by user_messages.id

我不知道这是否是一个完整的解决方案和某人(喜欧文)可能会填补丢失位。

I'm not sure if this is a complete solution and someone (hi Erwin) will probably fill in the missing bits.

您可以在这里保存自己的一些麻烦使用 系列 为你的 ID 列的数据类型。这将创建和挂钩的顺序给你。

You can save yourself some trouble here by using serial as the data type of your id column. That will create and hook up the sequence for you.

例如:

=> create sequence seq_test_id;
=> create table seq_test (id integer not null default nextval('seq_test_id'::regclass));
=> select pg_catalog.pg_get_serial_sequence('seq_test','id');
 pg_get_serial_sequence 
------------------------

(1 row)
=> alter sequence seq_test_id owned by seq_test.id;
=> select pg_catalog.pg_get_serial_sequence('seq_test','id');
 pg_get_serial_sequence 
------------------------
 public.seq_test_id
(1 row)

这篇关于新分配的顺序是不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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