method
pk_and_sequence_for
v1.2.6 -
Show latest stable
-
0 notes -
Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3
- 2.1.0 (9)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- What's this?
Related methods
- Class methods (9)
- create_unlogged_tables (>= v6.0.0)
- database_exists? (>= v6.0.0)
- datetime_type (>= v7.0.0)
- dbconsole (>= v7.1.3.2)
- initialize_type_map (>= v7.0.0)
- native_database_types (>= v7.0.0)
- new
- new_client (>= v6.1.3.1)
- visitor_for (>= v3.1.0)
- Instance methods (196)
- active?
- active_threadsafe? (>= v4.1.8)
- adapter_name
- add_column
- add_enum_value (>= v7.1.3.2)
- add_oid (>= v4.1.8)
- add_order_by_for_associatio...
- add_pg_decoders (>= v5.0.0.1)
- add_pg_encoders (>= v5.0.0.1)
- arel_visitor (>= v5.0.0.1)
- begin_db_transaction (>= v2.0.3)
- build_insert_sql (>= v6.0.0)
- build_statement_pool (>= v6.0.0)
- can_perform_case_insensitiv... (>= v5.0.0.1)
- cast_to_time
- change_column (>= v2.0.3)
- change_column_default (>= v2.0.3)
- change_column_null (>= v2.0.3)
- check_version (>= v6.0.0)
- clear_cache! (>= v3.1.0)
- client_min_messages (>= v2.0.3)
- client_min_messages= (>= v2.0.3)
- column_definitions
- column_name_for_operation (>= v4.2.1)
- columns (>= v2.0.3)
- commit_db_transaction (>= v2.0.3)
- configure_connection
- connect (>= v2.0.3)
- construct_coder (>= v5.0.0.1)
- create (>= v3.0.0)
- create_database (>= v2.1.0)
- create_enum (>= v7.0.0)
- create_savepoint (>= v2.3.8)
- create_table_definition (>= v4.0.2)
- current_database (>= v2.2.1)
- current_schema (>= v3.2.1)
- default_index_type? (>= v5.1.7)
- default_sequence_name
- default_value
- disable_extension (>= v4.0.2)
- disable_referential_integrity (>= v2.1.0)
- discard! (>= v5.2.3)
- disconnect!
- distinct
- drop_database (>= v2.1.0)
- drop_enum (>= v7.1.3.2)
- enable_extension (>= v4.0.2)
- encoding (>= v2.2.1)
- enum_types (>= v7.0.0)
- escape_bytea
- exec_cache (>= v3.1.0)
- exec_delete (>= v3.1.0)
- exec_no_cache (>= v3.1.0)
- exec_query (>= v3.1.0)
- exec_update (>= v3.1.0)
- execute (>= v2.0.3)
- execute_and_clear (>= v4.2.1)
- explain (>= v3.2.1)
- extension_available? (>= v6.0.0)
- extension_enabled? (>= v4.0.2)
- extensions (>= v4.0.2)
- extract_default_function (>= v4.2.1)
- extract_limit (>= v4.2.1)
- extract_pg_identifier_from_... (>= v2.3.8)
- extract_schema_and_table (>= v3.1.0)
- extract_table_ref_from_inse... (>= v3.2.1)
- extract_value_from_default (>= v4.2.1)
- get_advisory_lock (>= v5.0.0.1)
- get_database_version (>= v6.0.0)
- get_oid_type (>= v4.1.8)
- has_default_function? (>= v4.2.1)
- index_algorithms (>= v4.0.2)
- indexes (>= v2.0.3)
- index_name_length (>= v2.3.8)
- initialize_type_map (>= v4.0.2)
- insert (>= v2.0.3)
- insert_sql (>= v3.1.0)
- in_transaction? (>= v5.0.0.1)
- is_cached_plan_failure? (>= v5.0.0.1)
- last_insert_id
- last_insert_id_result (>= v4.0.2)
- last_insert_id_value (>= v4.0.2)
- load_additional_types (>= v4.2.1)
- load_types_queries (>= v7.0.0)
- lookup_cast_type (>= v4.2.1)
- max_identifier_length (>= v5.1.7)
- migration_keys (>= v4.0.2)
- native_database_types
- outside_transaction? (>= v2.3.8)
- pk_and_sequence_for
- postgresql_version (>= v2.0.3)
- prepare_column_options (>= v4.0.2)
- prepare_statement (>= v3.2.1)
- primary_key (>= v2.3.8)
- query (>= v2.1.0)
- quote
- quote_column_name
- quoted_date
- quote_string (>= v2.1.0)
- quote_table_name (>= v2.3.8)
- reconfigure_connection_time... (>= v7.1.3.2)
- reconnect (>= v7.1.3.2)
- reconnect!
- recreate_database (>= v2.1.0)
- release_advisory_lock (>= v5.0.0.1)
- release_savepoint (>= v2.3.8)
- reload_type_map (>= v4.0.2)
- remove_index (>= v2.0.3)
- remove_index! (>= v2.3.8)
- rename_column (>= v2.0.3)
- rename_enum (>= v7.1.3.2)
- rename_enum_value (>= v7.1.3.2)
- rename_index (>= v3.1.0)
- rename_table
- reset! (>= v3.1.0)
- reset_pk_sequence!
- result_as_array (>= v2.1.0)
- retryable_query_error? (>= v7.1.3.2)
- return_value_after_insert? (>= v7.1.3.2)
- rollback_db_transaction (>= v2.0.3)
- rollback_to_savepoint (>= v2.3.8)
- schema_creation (>= v4.0.2)
- schema_exists? (>= v3.2.1)
- schema_search_path (>= v2.0.3)
- schema_search_path= (>= v2.0.3)
- select
- select_raw (>= v2.0.3)
- select_rows (>= v2.0.3)
- serial_sequence (>= v3.1.0)
- session_auth= (>= v3.1.0)
- set_standard_conforming_str... (>= v2.3.8)
- sql_for_insert (>= v3.1.0)
- sql_key (>= v3.2.1)
- substitute_at (>= v3.1.0)
- supports_advisory_locks? (>= v5.0.0.1)
- supports_bulk_alter? (>= v5.2.3)
- supports_check_constraints? (>= v6.1.3.1)
- supports_comments? (>= v5.0.0.1)
- supports_common_table_expre... (>= v6.1.3.1)
- supports_datetime_with_prec... (>= v5.0.0.1)
- supports_ddl_transactions? (>= v2.2.1)
- supports_deferrable_constra... (>= v7.0.0)
- supports_disable_referentia... (>= v2.1.0)
- supports_exclusion_constrai... (>= v7.1.3.2)
- supports_explain? (>= v3.2.1)
- supports_expression_index? (>= v5.0.0.1)
- supports_extensions? (>= v4.0.2)
- supports_foreign_keys? (>= v4.2.1)
- supports_foreign_tables? (>= v5.2.3)
- supports_identity_columns? (>= v7.1.3.2)
- supports_index_include? (>= v7.1.3.2)
- supports_index_sort_order? (>= v3.2.1)
- supports_insert_conflict_ta... (>= v6.0.0)
- supports_insert_on_conflict? (>= v6.0.0)
- supports_insert_on_duplicat... (>= v6.0.0)
- supports_insert_on_duplicat... (>= v6.0.0)
- supports_insert_returning? (>= v6.0.0)
- supports_insert_with_return... (>= v2.2.1)
- supports_json? (>= v5.0.0.1)
- supports_lazy_transactions? (>= v6.0.0)
- supports_materialized_views? (>= v4.2.1)
- supports_migrations?
- supports_nulls_not_distinct? (>= v7.1.3.2)
- supports_optimizer_hints? (>= v6.0.0)
- supports_partial_index? (>= v4.0.2)
- supports_partitioned_indexes? (>= v6.1.3.1)
- supports_pgcrypto_uuid? (>= v5.1.7)
- supports_primary_key? (>= v2.3.8)
- supports_ranges? (>= v4.0.2)
- supports_restart_db_transac... (>= v7.1.3.2)
- supports_savepoints? (>= v2.3.8)
- supports_standard_conformin... (>= v2.0.3)
- supports_statement_cache? (>= v3.1.0)
- supports_transaction_isolat... (>= v4.0.2)
- supports_unique_constraints? (>= v7.1.3.2)
- supports_validate_constraints? (>= v5.2.3)
- supports_views? (>= v4.2.1)
- supports_virtual_columns? (>= v7.0.0)
- table_alias_length
- table_definition (>= v3.1.0)
- table_exists? (>= v3.0.0)
- tables (>= v2.0.3)
- transaction (>= v2.2.1)
- transaction_active? (>= v2.2.1)
- translate_exception (>= v3.0.0)
- translate_field_type
- truncate (>= v4.2.1)
- type_cast (>= v3.1.0)
- type_map (>= v4.1.8)
- type_to_sql (>= v2.0.3)
- unescape_bytea
- update_sql (>= v2.0.3)
- update_table_definition (>= v4.0.2)
- update_typemap_for_default_... (>= v6.0.0)
- use_insert_returning? (>= v4.0.2)
- valid_type? (>= v4.0.2)
= private
= protected
pk_and_sequence_for(table)
public
Find a table’s primary key and sequence.
Show source
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 270 def pk_and_sequence_for(table) # First try looking for a sequence with a dependency on the # given table's primary key. result = query("SELECT attr.attname, name.nspname, seq.relname\nFROM pg_class seq,\npg_attribute attr,\npg_depend dep,\npg_namespace name,\npg_constraint cons\nWHERE seq.oid = dep.objid\nAND seq.relnamespace = name.oid\nAND seq.relkind = 'S'\nAND attr.attrelid = dep.refobjid\nAND attr.attnum = dep.refobjsubid\nAND attr.attrelid = cons.conrelid\nAND attr.attnum = cons.conkey[1]\nAND cons.contype = 'p'\nAND dep.refobjid = '\#{table}'::regclass\n", 'PK and serial sequence')[0] if result.nil? or result.empty? # If that fails, try parsing the primary key's default value. # Support the 7.x and 8.0 nextval('foo'::text) as well as # the 8.1+ nextval('foo'::regclass). # TODO: assumes sequence is in same schema as table. result = query("SELECT attr.attname, name.nspname, split_part(def.adsrc, '''', 2)\nFROM pg_class t\nJOIN pg_namespace name ON (t.relnamespace = name.oid)\nJOIN pg_attribute attr ON (t.oid = attrelid)\nJOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)\nJOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])\nWHERE t.oid = '\#{table}'::regclass\nAND cons.contype = 'p'\nAND def.adsrc ~* 'nextval'\n", 'PK and custom sequence')[0] end # check for existence of . in sequence name as in public.foo_sequence. if it does not exist, return unqualified sequence # We cannot qualify unqualified sequences, as rails doesn't qualify any table access, using the search path [result.first, result.last] rescue nil end