# JuMP 19.0: constraint\_by\_name fails for vectorized constraints

**URL:** <https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 5, 2019, 12:28am UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481 "2019-03-05T00:28:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)\
**Post date:** [March 5, 2019, 12:28am UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/1 "2019-03-05T00:28:13Z")

</div>

```julia
using JuMP
m = Model()
@variable(m, x[1:2])
@constraint(m, mycons, x .== 0)
cn = constraint_by_name(m, "mycons") # returns nothing

```

Should `cn` be `nothing`?  
I expected it would return an array of constraint references.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 5, 2019, 10:05am UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/2 "2019-03-05T10:05:25Z")

</div>

Names are currently ignored for vectorized constraints, see

> <https://github.com/jump-dev/JuMP.jl/issues/1166>
>
> https://github.com/JuliaOpt/JuMP.jl/blob/c9200d7a108924bca2a703fcf12231867531393…3/src/macros.jl#L326

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [March 5, 2019, 1:18pm UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/3 "2019-03-05T13:18:55Z")

</div>

Even when that issue is resolved, `constraint_by_name(m, "mycons")` would not return an array of constraints. The _name attribute_ applies to individual constraints, not to groups of them ([Constraints · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.0/constraints/#Constraint-names-1)). `m[:mycons]` is the way to look up Julia variables stored in the model scope (Point 3 at [Constraints · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.0/constraints/#The-@constraint-macro-1)).

---

<div class="post-metadata">

**Author:** ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)\
**Post date:** [March 5, 2019, 3:17pm UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/4 "2019-03-05T15:17:01Z")

</div>

Thanks for the clarification.  
Part of my desired usage was to test for whether a model has a constraint in it.  
I thought constraint\_by\_name was the right way to do this because:

```julia
using JuMP
m = Model()
@variable(m, x)
@constraint(m, mycons, x == 0)
a = constraint_by_name(m, "mycons")
b = m[:mycons]
println(a == b) # true

a = constraint_by_name(m, "mycons2")
println(isnothing(a)) # true
b = m[:mycons2] # KeyError

```

I couldn’t figure out from the documentation a way to check whether `mycons` is defined using the symbol `:mycons`. I thought perhaps `haskey` but that’s not defined for a JuMP model. Any suggestions on the right way to do this?

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 5, 2019, 3:33pm UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/5 "2019-03-05T15:33:44Z")

</div>

You can do `haskey(object_dictionary(model), :mycons)`

---

<div class="post-metadata">

**Author:** ![torgo](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@torgo](https://discourse.julialang.org/u/torgo)\
**Post date:** [March 5, 2019, 5:13pm UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/6 "2019-03-05T17:13:08Z")

</div>

Perfect, thanks. I didn’t see this function anywhere in the JuMP documentation.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 5, 2019, 6:15pm UTC](https://discourse.julialang.org/t/jump-19-0-constraint-by-name-fails-for-vectorized-constraints/21481/7 "2019-03-05T18:15:38Z")

</div>

It would be nice if `haskey` was defined. I think it’s a pretty obvious thing that people are going to try and run into issues. I made an issue: [Define haskey on model · Issue #1887 · jump-dev/JuMP.jl · GitHub](https://github.com/JuliaOpt/JuMP.jl/issues/1887)
