Single table inheritance introduce on mid of development.

Hi Folks,

  1. I converting existing model to STI
  2. Existing model CtctContact
  3. ctct_contacts rename as contacts and add column called type in table.
  4. In model modified base class(CtctContact < Contact) during migration it’s show contacts table not found like that.

Any idea how to fix.

Thanks,

Dhamodharan.N

Do you mean that it is the migration to change the table name and add
the type column that is failing?
Show us the failing code (just the section that is failing) and the
exact error message and stack trace.

Colin

Hi Colin Law,

Existin code ctct_contact.rb => class CtctContact < Contact

After changes ctct_contact.rb => class CtctContact < ActiveRecord::Base

Above changes works fine for me but we cant proceed with that changes right?

Trace :

– add_index(:ctct_contacts, [:ctct_account_id, :email_address], {:unique=>true})
-> 0.0732s
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR: relation “contacts” does not exist
LINE 4: WHERE a.attrelid = ‘“contacts”’::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull

          FROM pg_attribute a LEFT JOIN pg_attrdef d
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum
         WHERE a.attrelid = '"contacts"'::regclass
           AND a.attnum > 0 AND NOT a.attisdropped

         ORDER BY a.attnum

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in async_exec' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in exec_no_cache’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in block in exec_query' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in block in log’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in instrument' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:275:in log’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/instrumentation/active_record.rb:32:in block in log_with_newrelic_instrumentation' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:242:in trace_execution_scoped’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/instrumentation/active_record.rb:29:in log_with_newrelic_instrumentation' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in exec_query’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1276:in column_definitions' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:859:in columns’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/schema_cache.rb:12:in block in initialize' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/model_schema.rb:228:in yield’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/model_schema.rb:228:in default' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/model_schema.rb:228:in columns’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/model_schema.rb:237:in columns_hash' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/inheritance.rb:19:in descends_from_active_record?’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/inheritance.rb:25:in finder_needs_type_condition?' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/base.rb:455:in relation’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/scoping/named.rb:37:in scoped' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/querying.rb:5:in find’

/home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:14:in block in obfuscate_current_contacts' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in transaction’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in transaction' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:493:in block in transaction_with_trace_ActiveRecord_self_name_transaction’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:242:in trace_execution_scoped' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:488:in transaction_with_trace_ActiveRecord_self_name_transaction’

/home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:13:in obfuscate_current_contacts' /home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:9:in change’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:407:in block (2 levels) in migrate' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:407:in block in migrate’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in with_connection' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:389:in migrate’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:528:in migrate' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:720:in block (2 levels) in migrate’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in call' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in block in ddl_transaction’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in transaction' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in transaction’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:493:in block in transaction_with_trace_ActiveRecord_self_name_transaction' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:242:in trace_execution_scoped’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/newrelic_rpm-3.5.4.33/lib/new_relic/agent/method_tracer.rb:488:in transaction_with_trace_ActiveRecord_self_name_transaction' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in ddl_transaction’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:719:in block in migrate' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:700:in each’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:700:in migrate' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:570:in up’

/home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/migration.rb:551:in migrate' /home/dhamodharan/.rvm/gems/ruby-1.9.2-p180@application20/gems/activerecord-3.2.6/lib/active_record/railties/databases.rake:153:in block (2 levels) in <top (required)>’

Hi Colin Law,

Existin code ctct_contact.rb => class CtctContact < Contact

After changes ctct_contact.rb => class CtctContact < ActiveRecord::Base

Above changes works fine for me but we cant proceed with that changes right?

I don't understand what you mean. I thought you were moving /to/ STI,
not away from it.

Trace :

-- add_index(:ctct_contacts, [:ctct_account_id, :email_address],
{:unique=>true})
   -> 0.0732s
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR: relation "contacts" does not exist

Show us the whole migration code please.

Colin

Migration1:
class RenameTableForMailchimp < ActiveRecord::Migration
def change
rename_table :ctct_contacts, :contacts
end
end

Migration2:
class AddTypeColumnForMailchimp < ActiveRecord::Migration

def change
add_column :contacts, :type, :string
end
end

Migration1:
class RenameTableForMailchimp < ActiveRecord::Migration
  def change
    rename_table :ctct_contacts, :contacts
  end
end

Migration2:
class AddTypeColumnForMailchimp < ActiveRecord::Migration
  def change
    add_column :contacts, :type, :string
    end
end

Please don't top post, it makes it difficult to follow the thread.
Insert your replies inline at appropriate points.

The error you showed came from
/home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:9:in
`change' so it might be helpful to show that migration. I presume the
migrations above ran successfully and that you have checked in the
database that the table has been renamed and the type column added ok.

Colin

> Migration1:
> class RenameTableForMailchimp < ActiveRecord::Migration
> def change
> rename_table :ctct_contacts, :contacts
> end
> end
>
> Migration2:
> class AddTypeColumnForMailchimp < ActiveRecord::Migration
> def change
> add_column :contacts, :type, :string
> end
> end

Please don't top post, it makes it difficult to follow the thread.
Insert your replies inline at appropriate points.

The error you showed came from

/home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:9:in
`change' so it might be helpful to show that migration. I presume the
migrations above ran successfully and that you have checked in the
database that the table has been renamed and the type column added ok.

Thanks Lot Colin Law,

Cause of issue is in that migration have data migration script(like
invoking CtctContact model) but current code CtctContact base class Contact
so it try to search contact table.

Colin

Sorry, I still don't know what you mean as you still have not posted
the code that is generating the error. If you still have a problem
post the full code of the migration rb file and the full error that
you are seeing when you run rake db:migrate.

Colin

>
>
>
>>
>> > Migration1:
>> > class RenameTableForMailchimp < ActiveRecord::Migration
>> > def change
>> > rename_table :ctct_contacts, :contacts
>> > end
>> > end
>> >
>> > Migration2:
>> > class AddTypeColumnForMailchimp < ActiveRecord::Migration
>> > def change
>> > add_column :contacts, :type, :string
>> > end
>> > end
>>
>> Please don't top post, it makes it difficult to follow the thread.
>> Insert your replies inline at appropriate points.
>>
>> The error you showed came from
>>
>>
/home/dhamodharan/project/application20Web/db/migrate/20121128075457_add_ctct_contact_uniqueness.rb:9:in
>> `change' so it might be helpful to show that migration. I presume the
>> migrations above ran successfully and that you have checked in the
>> database that the table has been renamed and the type column added ok.
>>
>
> Thanks Lot Colin Law,
>
> Cause of issue is in that migration have data migration script(like
> invoking CtctContact model) but current code CtctContact base class
Contact
> so it try to search contact table.

Sorry, I still don't know what you mean as you still have not posted
the code that is generating the error. If you still have a problem
post the full code of the migration rb file and the full error that
you are seeing when you run rake db:migrate.

Colin

   sample code:
  def change
   add_index :ctct_contacts, [:ctct_account_id, :email_address], :unique =>
true
    obfuscate_current_contacts
  end

  def obfuscate_current_contacts
    CtctContact.transaction do
      CtctContact.find(:all).each do |c|
              #some code with c object
      end
    end
  end

  Problem solved commenting obfuscate_current_contacts method invoking.
  Cause of issue we are invoking current code base CtctContact base class
Contact so its seeking contacts table.
But our table rename migration later.

Thanks,
Dhamodharan