Gurobi how to get variable coefficients in constraints

Okay here is an interesting one. Your method returns an error if the constraint does not contain that variable. For example:

cc[1,1] # segment_power[1,1] : p_g_seg[1,1,1] + p_g_seg[1,2,1] + p_g_seg[1,3,1] - p_g[1,1] == 0.0
con = constraint_object(cc[1,1])
con.func.terms
#=
OrderedCollections.OrderedDict{VariableRef,Float64} with 4 entries:
  p_g_seg[1,1,1] => 1.0
  p_g_seg[1,2,1] => 1.0
  p_g_seg[1,3,1] => 1.0
  p_g[1,1]       => -1.0
=#
haskey(con.func.terms, "p_g[1,1]") #false
haskey(con.func.terms, p_g[1,1]) #false
collect(keys(con.func.terms))
#=
4-element Array{VariableRef,1}:
 p_g_seg[1,1,1]
 p_g_seg[1,2,1]
 p_g_seg[1,3,1]
 p_g[1,1]
=#
p_g[1,1] in collect(keys(con.func.terms)) #false
typeof(collect(keys(con.func.terms))[4]) #VariableRef
typeof(p_g[1,1]) #VariableRef
con.func.terms[p_g[1,1]] # -1.00
con.func.terms[p_g[1,2]] #KeyError: key p_g[1,2] not found

Besides your suggestion, I also found How do I extract the coefficients and RHS's of all constraints in a JuMP linear program? (Need for KKT) - #4 by odow and it suggested using some NLP package that returns “A” matrix. However, it is quite a challenge to figure out the ordering of that A matrix in large cases. Do you have any idea how to get around this issue?