rmre icon indicating copy to clipboard operation
rmre copied to clipboard

Incorrect models generated for Postgresql

Open shapiro2 opened this issue 11 years ago • 3 comments

RMRE is generating invalid models for postgresql when there are tables with the same names in multiple schemas that have foreign keys from one schema to another. The specifics are

I have tables in schema called "acct" and "merge". The ones in "merge" have FK into the table in "acct". I am asking RMRE to generate models for only the tables in "acct" (see rmre.yml below), but it sees the FK from "models" to the "acct" tables.


CREATE TABLE acct.people
(
  person_id serial NOT NULL,
  first_name character varying(100),
  middle_name character varying(60),
  last_name character varying(100) NOT NULL,
  CONSTRAINT pk_people PRIMARY KEY (person_id)
);

CREATE TABLE acct.people_map
(
  people_map_id serial NOT NULL,
  person_id integer NOT NULL,
  organization_id integer NOT NULL,
  site_person_id text NOT NULL,
  CONSTRAINT pk_people_map PRIMARY KEY  (people_map_id),
  CONSTRAINT fk1_people_map FOREIGN KEY (organization_id) REFERENCES acct.organizations (organization_id),
  CONSTRAINT fk2_people_map FOREIGN KEY (person_id) REFERENCES acct.people (person_id)
);

CREATE TABLE acct.organizations
(
  organization_id serial NOT NULL,
  organization_name text NOT NULL,
  CONSTRAINT pk_organizations PRIMARY KEY (organization_id)
)


CREATE TABLE merge.people
(
  id serial NOT NULL,
  keep_person_id integer NOT NULL,
  delete_person_id integer NOT NULL,
  CONSTRAINT people_pk PRIMARY KEY (id)
);

CREATE TABLE merge.people_map
(
  id integer NOT NULL,
  organization_id integer NOT NULL,
  keep_site_person_id text NOT NULL,
  delete_site_person_id text NOT NULL,
  CONSTRAINT people_map_pk PRIMARY KEY (id, organization_id),
  CONSTRAINT people_map_fk1 FOREIGN KEY (id) REFERENCES merge.people (id),
  CONSTRAINT people_map_fk2 FOREIGN KEY (organization_id) REFERENCES acct.organizations (organization_id)
);


=== rmre.yml
db:
  :adapter: postgresql
  :host: <host>
  :username: <username>
  :password: <passwd>
  :timeout: 5000
  :database: <db>
  :schema_search_path: "acct"

=== models generated by RMRM

=== person.rb
class Person < ActiveRecord::Base
    self.primary_key = :person_id

    has_many :people_maps, :class_name => 'PeopleMap', :foreign_key => :person_id    
    has_many :people_maps, :class_name => 'PeopleMap'    
end

=== people_map.rb
class PeopleMap < ActiveRecord::Base
    self.table_name = 'people_map'
    self.primary_key = :people_map_id

    belongs_to :organization, :class_name => 'Organization', :foreign_key => :organization_id    
    belongs_to :person, :class_name => 'Person', :foreign_key => :person_id    
    belongs_to :person, :class_name => 'Person', :foreign_key => :id 
    belongs_to :organization, :class_name => 'Organization', :foreign_key => :organization_id    
end

=== organization.rb
class Organization < ActiveRecord::Base
    self.primary_key = :organization_id

    has_many :people_maps, :class_name => 'PeopleMap', :foreign_key => :organization_id    
    has_many :people, :class_name => 'Person', :foreign_key => :organization_id    
    has_many :people_maps, :class_name => 'PeopleMap', :foreign_key => :organization_id    
end

shapiro2 avatar Jul 14 '13 15:07 shapiro2

It generates empty models in my case. Is anybody maintaining the repo?

jonarrien avatar Oct 11 '13 06:10 jonarrien

I am, but due to other obligations I cannot find time to fix things quickly. How do you invoke rare? Can you give more informations about your tables (do you use multiple schemas, do you use schemas at all...). You must give more details.

bosko avatar Oct 11 '13 07:10 bosko

bosko, jonarrien,

I suspect that joanrrien's tables are in a schema that is not in the Postgres search path. Unless the search_path is explicitly set, the search_path defaults to ,public. For example, if the login is 'xxx', the the search path is xxx,public. If the tables are in schema 'yyy', then RMRE will not see them.

You'd have to set the search path in your rmre.yml file: :schema_search_path: "yyy"

Either that or the tables or the schema don't allow access for the login

shapiro2 avatar Oct 11 '13 10:10 shapiro2