# In JuMP, how to access a named constraint created inside a function

**URL:** <https://discourse.julialang.org/t/in-jump-how-to-access-a-named-constraint-created-inside-a-function/56912>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 10, 2021, 8:53pm UTC](https://discourse.julialang.org/t/in-jump-how-to-access-a-named-constraint-created-inside-a-function/56912 "2021-03-10T20:53:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [March 10, 2021, 8:53pm UTC](https://discourse.julialang.org/t/in-jump-how-to-access-a-named-constraint-created-inside-a-function/56912/1 "2021-03-10T20:53:11Z")

</div>

I would like to delete a constraint created inside a function. For example,

```julia
function mymodel(a)
    m = Model()
    @variable(model, w[1:n] >= 0)
    @constraint(model, budget, sum(w) == a)
        ...
    return m
end
model = mymodel(1)
delete(model, budget)

```

throws `ERROR: LoadError: UndefVarError: budget not defined`  
How may I access the constraint budget from outside the function?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 10, 2021, 10:12pm UTC](https://discourse.julialang.org/t/in-jump-how-to-access-a-named-constraint-created-inside-a-function/56912/2 "2021-03-10T22:12:39Z")

</div>

`@constraint` creates a perfectly normal variable inside the scope it is used. You can deal with it as you would with any other variable (for example, returning it from inside the function).

Nevertheless, `@constraint` also register any named constraint inside the model (as `@variable` does with variables), so you can access it by using `model[:budget]` too. I use it all the time, it is a very practical feature. `delete` will take care of removing `budget` from this listing too.

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [March 10, 2021, 10:54pm UTC](https://discourse.julialang.org/t/in-jump-how-to-access-a-named-constraint-created-inside-a-function/56912/3 "2021-03-10T22:54:02Z")

</div>

Perfect! `delete(model, model[:budget])` worked fine. Thank you very much
