# Can I name new JuMP constraints in a function using an argument?

**URL:** <https://discourse.julialang.org/t/can-i-name-new-jump-constraints-in-a-function-using-an-argument/95640>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 6, 2023, 11:20pm UTC](https://discourse.julialang.org/t/can-i-name-new-jump-constraints-in-a-function-using-an-argument/95640 "2023-03-06T23:20:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sallyrobson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sallyrobson/32/47442_2.png) [@sallyrobson](https://discourse.julialang.org/u/sallyrobson)\
**Post date:** [March 6, 2023, 11:20pm UTC](https://discourse.julialang.org/t/can-i-name-new-jump-constraints-in-a-function-using-an-argument/95640/1 "2023-03-06T23:20:26Z")

</div>

I’m trying to define a function that would add a JuMP constraint to a model, and I would like to set the name of the constraint using an argument from the function. When I have tried to do this in the past it just sets the name of the constraint to be the name of the argument instead of the value of the argument. And if I run the function multiple times it will error because the named constraint already exists.

A simplified example function would look like this:

```julia
function add_constraint!(model, cons_name, value, array, range)
   
   @constraint(model, cons_name[r in 1:length(range)], array[r] <= value)

end

```

In the code above, I would like the name of the constraint to be whatever get’s passed into the function as the cons\_name argument. How can I do that?

Thanks!

---

<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 6, 2023, 11:26pm UTC](https://discourse.julialang.org/t/can-i-name-new-jump-constraints-in-a-function-using-an-argument/95640/2 "2023-03-06T23:26:34Z")

</div>

Hi there, welcome to the forum.

See this part of the docs: [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#String-names,-symbolic-names,-and-bindings).

If `cons_name` is a `String`, you’re probably looking for something like:

```julia
function add_constraint!(model, cons_name::String, value, array, range)
    return model[Symbol(cons_name)] = @constraint(
        model,
        [r in 1:length(range)],
        array[r] <= value,
        base_name = cons_name,
    )
end

```

---

<div class="post-metadata">

**Author:** ![sallyrobson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sallyrobson/32/47442_2.png) [@sallyrobson](https://discourse.julialang.org/u/sallyrobson)\
**Post date:** [May 9, 2023, 1:30pm UTC](https://discourse.julialang.org/t/can-i-name-new-jump-constraints-in-a-function-using-an-argument/95640/3 "2023-05-09T13:30:09Z")

</div>

Thank you! This worked
