Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1273 (closed enhancement: fixed)

Opened 4 years ago

Last modified 2 years ago

[PATCH] sequence names for PostgreSQL

Reported by: yrashk@gmail.com Assigned to: rails@bitsweat.net
Priority: normal Milestone: 1.0
Component: ActiveRecord Version: 0.12.1
Severity: normal Keywords: default sequence postgresql
Cc: rails@bitsweat.net

Description

This patch provides a way to retrieve sequence name from pg_attrdef. Currently AR makes assumption on the default sequence naming.

Attachments

pgsql_seq.diff (2.6 kB) - added by yrashk@gmail.com on 05/07/05 14:32:43.
Quick solution
fixtures.diff (1.8 kB) - added by yrashk@gmail.com on 05/15/05 13:13:59.
This patch fixes reset_sequences in fixtures.rb in order to work properly with tables that shares one sequence (that was an original intent of the ticket) and uses #{pk} instead of id. The previous attachment should be limited to patching postgresql_adapter.rb only

Change History

05/07/05 14:32:43 changed by yrashk@gmail.com

  • attachment pgsql_seq.diff added.

Quick solution

05/07/05 23:37:03 changed by anonymous

  • summary changed from (PATCH) sequence names for PostgreSQL to [PATCH] sequence names for PostgreSQL.

05/15/05 13:13:59 changed by yrashk@gmail.com

  • attachment fixtures.diff added.

This patch fixes reset_sequences in fixtures.rb in order to work properly with tables that shares one sequence (that was an original intent of the ticket) and uses #{pk} instead of id. The previous attachment should be limited to patching postgresql_adapter.rb only

05/21/05 01:06:15 changed by bitsweat

  • cc set to rails@bitsweat.net.
  • keywords set to default sequence postgresql.
  • severity changed from normal to enhancement.

I like the idea of asking the database which sequence is used as the default value for a column, but I have reservations regarding this patch.

The sequence_name method is implemented twice: once in the pg adapter and once in fixtures. You can use the connection's method in fixtures.

The caching in sequence_name is roundabout and the lookup itself is performed in three queries instead of one.

*There are no unit tests.*

06/16/05 15:54:37 changed by yrashk@gmail.com

Jeremy,

If the basic idea of patch will be preliminary agreed to add to Rails, I'll refactor it and add unit tests.

06/16/05 16:09:22 changed by bitsweat

Please do! Also, note that the PostgreSQL connection adapter has changed a bit: schema support was recently committed to trunk.

06/16/05 18:43:04 changed by yrashk@gmail.com

Ok! I'll do it within a week or so. Thanks for the support!

06/21/05 06:02:40 changed by anonymous

  • owner changed from David to rails@bitsweat.net.

06/22/05 21:01:51 changed by flash@vanklinkenbergsoftware.nl

There's a patch in ticket #962 that takes care of the fixtures problem and does sequence name lookup in one query instead of three. Maybe this approach is useful in conjunction with the above patches...

08/20/05 03:05:02 changed by robbyrussell

This patch will not work if you have multiple SCHEMAS in PostgreSQL with the same table name and primary key column name.

For example:

=# \d legacy.people
                                       Table "legacy.people"
  Column   |         Type          |                           Modifiers                           
-----------+-----------------------+---------------------------------------------------------------
 people_id | integer               | not null default nextval('legacy.people_people_id_seq'::text)
 name      | character varying(50) | 
Indexes:
    "people_pkey" PRIMARY KEY, btree (people_id)

=# \d foo.people
                                       Table "foo.people"
  Column   |         Type          |                         Modifiers                          
-----------+-----------------------+------------------------------------------------------------
 people_id | integer               | not null default nextval('foo.people_people_id_seq'::text)
 name      | character varying(50) | 
Indexes:
    "people_pkey" PRIMARY KEY, btree (people_id)      

This part of his patch will return multiple records now:

=# SELECT oid FROM pg_class WHERE relname = 'people';
  oid  
-------
 17526
 17517
(2 rows) 

If the following query, the result will look like this.

=# SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'people');
ERROR:  more than one row returned by a subquery used as an expression  

The solution that I posted in ticket #2016, helps get around this problem, but does not work with non-standard sequence names. I have yet to find a solution for that easily within the PostgreSQL system tables.

09/26/05 21:07:45 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

Superseded by #2016.