2015-12-13 9 views
5

मैं एक्टो पोस्टग्रेस में 2 फ़ील्ड अद्वितीय बाधा करने की कोशिश कर रहा हूं।2 फ़ील्ड पर अद्वितीय_कॉन्स्ट्रेन पहचान नहीं किया जा रहा है

प्रवासन में:

create unique_index(:presences, [:event_id, :user_id], name: :special_event_user) 

ChangeSet: अब तक मैं ऐसा करने में कामयाब रहे

def changeset(presence, params \\ :empty) do 
    presence 
     |> cast(params, @required_fields, @optional_fields) 
     |> foreign_key_constraint(:user_id) 
     |> foreign_key_constraint(:event_id) 
     |> unique_constraint(:event_id, name: :special_event_user) 
    end 

भी करने की कोशिश की:

|> unique_constraint(:special_event_user) 

लेकिन मैं बार आ रही है:

If you would like to convert this constraint into an error, please 
call unique_constraint/3 in your changeset and define the proper 
constraint name. The changeset has not defined any constraint. 

जो मैंने सोचा कि मैंने पहले ही किया है। कोई सुझाव?

संपादित करें:

कार्रवाई:

def assign(conn, %{"event" => event_id}) do 
    case Integer.parse(event_id) do 
     {val, _ } -> 
      case Repo.one(User.unique_user(User, get_session(conn, :login))) do 
      nil -> conn 
       |> put_flash(:error, "Błąd bazy danych") 
       |> redirect(to: "/event") 
      result -> 
       presence = %Presence{ user_id: result.id, event_id: val, state: Presence.get_assigned } |> Repo.insert! 
       conn 
       |> put_flash(:info, "Zostałeś zapisany na wydarzenie") 
       |> redirect(to: "/event") 
      end 
    end 
    end 
ऊपर कोड से

Egzample उपस्थिति जब कोई त्रुटि है:

%Kpsz.Model.Presence{__meta__: #Ecto.Schema.Metadata<:loaded>, 
event: #Ecto.Association.NotLoaded<association :event is not loaded>, 
event_id: 1, id: 1, inserted_at: #Ecto.DateTime<2015-12-14T15:45:39Z>, 
state: 1, updated_at: #Ecto.DateTime<2015-12-14T15:45:39Z>, 
user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1} 

संपादित करें: अब मैं हो रही है:

constraint error when attempting to insert model: 

    * unique: special_event_user 

If you would like to convert this constraint into an error, please 
call unique_constraint/3 in your changeset and define the proper 
constraint name. The changeset defined the following constraints: 

    * unique: presences_special_event_user_index 
    * foreign_key: presences_event_id_fkey 
    * foreign_key: presences_user_id_fkey 
+2

आपकी परिभाषा Ecto दस्तावेज में उदाहरण के मैच के लिए लगता है। इंडेक्स नाम को सीधे बाधा में पारित करने की कोशिश करें जैसे कि यह इस ब्लॉग में किया गया था (http://blog.praveenperera.com/using-compound-unique-indexes-to-validate-uniqueness-of-ecto-associations) , यानी 'unique_constraint (: special_event_user)' – AbM

+0

मेरे लिए वही बात ... – Haito

+1

क्या आप रिकॉर्ड बनाने के लिए उपयोग कर रहे कोड को प्रदान कर सकते हैं? – Gazler

उत्तर

7

आप नहीं जा रहे हैं आपके बदलाव समारोह।

presence = 
    %Presence{ user_id: result.id, event_id: val, state: Presence.get_assigned } 
    |> Repo.insert! 

होना चाहिए:

presence = 
    Presence.changeset(%Presence{), %{user_id: result.id, event_id: val, state: Presence.get_assigned}) 
    |> Repo.insert! 
+1

खैर .. दिमागी प्रतिलिपि कभी-कभी दर्दनाक होती है। आपके समय महोदय के लिए बहुत बहुत धन्यवाद। – Haito

+0

मुख्य पोस्ट के नीचे एक और नजर डालें? मुझे एक और डरावनी त्रुटि मिल रही है। यह अभी भी मुझे बताता है कि मैंने उस समारोह को नहीं बुलाया है। लेकिन इस बार यह बुलाए गए कार्यों के नाम को संकेत देता है जहां एक का उपयोग किया जाता है। – Haito

+0

यदि आपके पास बाधा त्रुटि है और 'Repo.insert'' के बजाय 'Repo.insert!' का उपयोग करें, तो यह उठाएगा। क्या यह हो रहा है? – Gazler

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