2010-06-19 14 views
20

मैं डीबी को रेक करने की कोशिश कर रहा हूं: माइग्रेशन मेरे हेर्कू इंस्टेंस में और मुझे एक त्रुटि मिलती है। एफएक्यू ने मेरी त्रुटि को नीचे वर्णित किया:मैं हेरोोकू में कॉलम प्रकार कैसे बदलूं?

Cannot change column type

Example: PGError: ERROR: column “verified_at” cannot be cast to type “date”

Cause: PostgreSQL doesn’t know how to cast all the rows in that table to the specified type. Most likely it means you have an integer or a string in that column.

Solution: Inspect your records and make sure they can be converted to the new type. Sometimes it’s easier to just avoid using change_column, renaming/creating a new column instead.

अब मैं इस माइग्रेशन को कैसे बदलूं। यह समस्या है जो मेरे पास है।

t.string :date_entered 

एक बाद माइग्रेशन में, मैं निम्नलिखित है:

change_column :contacts, :date_entered, :date 

यह change_column समस्या प्रतीत होता है जो मेरे संपर्क तालिका के लिए, मैं निम्नलिखित बनाया।

क्या मुझे ... माइग्रेशन के हाथ से बदलना चाहिए? क्या कोई तरीका है कि मैं अपने टेबल में डेटा साफ़ कर सकता हूं (मुझे नहीं पता था कि हेरोोकू टेबल में डेटा को पहचान लेगा क्योंकि मैं रेक कर रहा हूं)।

मुझे स्पष्ट रूप से इस मूल्य को बदलने की आवश्यकता है और यह मेरे पूरे एप्लिकेशन में उपयोग किया जाता है। धन्यवाद।

यही वह है जो मैं कोशिश कर रहा हूं ... विचार?

def self.up 
    #change_column :contacts, :date_entered, :date 
    #this fails in postgres, so trying the same outcome 

    rename_column :contacts, :date_entered, :date_entered_old 
    add_column :contacts, :date_entered, :date 
    remove_column :contacts, :date_entered_old 
end 

def self.down 
    add_column :contacts, :date_entered_old 
    remove_column :contacts, :date_entered 
    rename_column :contacts, :date_entered_old, :date_entered 
end 

उत्तर

39

निम्नलिखित है:

  1. स्तंभ एक
  2. तारीख के रूप में नया स्तंभ बी बनाने का नाम बदलने
  3. कदम एक से डेटा
  4. बी को एक
को दूर

दूसरे शब्दों में

def self.up 
    rename_column :contacts, :date_entered, :date_entered_string 
    add_column :contacts, :date_entered, :date 

    Contact.reset_column_information 
    Contact.find_each { |c| c.update_attribute(:date_entered, c.date_entered_string) } 
    remove_column :contacts, :date_entered_string 
end 
+0

#reset_column_information के लिए +1, जो मैंने पहले कभी नहीं देखा था का एक संशोधित और परीक्षण संस्करण है। ऐसा लगता है कि यह आवश्यक होने पर उन दुर्लभ समयों में आसान होगा। – jdl

+0

ditto re: रीसेट करें। – Angela

+1

अभी भी दो साल बाद काम करता है! –

1

यह सिमोन Carletti के समाधान

class ModifyContacts < ActiveRecord::Migration 
    def self.up 
    rename_column :contacts, :date_entered, :date_entered_string 
    add_column :contacts, :date_entered, :date 

    Contact.reset_column_information 
    Contact.find(:all).each { |contact| contact.update_attribute(:date_entered, contact.date_entered_string) } 
    remove_column :contacts, :date_entered_string 
    end 
end 
+1

हालांकि आप चाहते हैं कि 'find_each'। आपका संस्करण पूरी तालिका को स्मृति में पढ़ेगा, जबकि find_each उस समय कुछ पंक्तियों को प्राप्त करेगा। – clacke

संबंधित मुद्दे