# Adding a new variable to an existing variable array

**URL:** <https://discourse.julialang.org/t/adding-a-new-variable-to-an-existing-variable-array/17390>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [November 10, 2018, 4:26pm UTC](https://discourse.julialang.org/t/adding-a-new-variable-to-an-existing-variable-array/17390 "2018-11-10T16:26:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![borelian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/borelian/32/4723_2.png) [@borelian](https://discourse.julialang.org/u/borelian)\
**Post date:** [November 10, 2018, 4:26pm UTC](https://discourse.julialang.org/t/adding-a-new-variable-to-an-existing-variable-array/17390/1 "2018-11-10T16:26:16Z")

</div>

Hi. I’m teaching Column Generation on Jump. Hence, given a set of columns x[1:N] I’ll like add a new variable x[N+1] to the previous variable set.

If I try `@defVar(master, x[N+1]>=0, objective=1)` JUMP assumes that I’m adding a new set of variables (Error: Can only create one variable at a time when adding to existing constraints.)

I know that you can add the extra variable with a new name (z for example) or an anonymous variable, and iterate adding new variables at each step (and it works, even if all new variables have the same name), but I’ll like to add the new variable to the previous set x, or at least, to give a new name to each new variable. Is is possible?

---

<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:** [November 10, 2018, 7:42pm UTC](https://discourse.julialang.org/t/adding-a-new-variable-to-an-existing-variable-array/17390/2 "2018-11-10T19:42:04Z")

</div>

Hi @borelian,

It looks like you’re using a very old version of JuMP! The current syntax is [`@variable`](http://www.juliaopt.org/JuMP.jl/0.18/refvariable.html).

`x` is just a vector, so you can `push!` new elements onto it. You can also set the “name” of the variable using the `basename` keyword. See below:

```julia
model = Model()
N = 5
@variable(model, x[1:N] >= 0)
@constraint(model, con, sum(x) <= 1)
@objective(model, Max, sum(i* x[i] for i in 1:N))
push!(x, @variable(model, 
    lowerbound=0, 
    objective = N+1, 
    basename = "x[$(N+1)]", 
    inconstraints=[con], 
    coefficients=[1.0]
))

```
