diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql
index 759e657..f3353d6 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql
@@ -1,4 +1,5 @@
-SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype
+SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype,
+att.attnotnull as not_null, att.atthasdef as has_default_val
FROM pg_attribute att
JOIN pg_type ty ON ty.oid=atttypid
JOIN pg_namespace tn ON tn.oid=ty.typnamespace
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql
index 7536a9c..4f1de2a 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql
@@ -1,4 +1,5 @@
-SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype
+SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype,
+att.attnotnull as not_null, att.atthasdef as has_default_val
FROM pg_attribute att
JOIN pg_type ty ON ty.oid=atttypid
JOIN pg_namespace tn ON tn.oid=ty.typnamespace
diff --git a/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js b/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js
index cdfba4d..0505f40 100644
--- a/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js
+++ b/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js
@@ -110,7 +110,12 @@
// When text editor opens
this.loadValue = function (item) {
- if (item[args.column.pos] === "") {
+ var col = args.column;
+
+ if (_.isUndefined(item[args.column.pos]) && col.has_default_val) {
+ $input.val("");
+ }
+ else if (item[args.column.pos] === "") {
$input.val("''");
}
else {
@@ -145,7 +150,10 @@
};
this.isValueChanged = function () {
- return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
+ // Use _.isNull(value) for comparison for null instead of
+ // defaultValue == null, because it returns true for undefined value.
+ return (!($input.val() == "" && _.isNull(defaultValue))) &&
+ ($input.val() != defaultValue);
};
this.validate = function () {
diff --git a/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js b/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js
index 290bddd..bfd66e8 100644
--- a/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js
+++ b/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js
@@ -66,7 +66,14 @@
}
function TextFormatter(row, cell, value, columnDef, dataContext) {
- if (_.isUndefined(value) || value === null) {
+ // If column has default value, set placeholder
+ if (_.isUndefined(value) && columnDef.has_default_val) {
+ return "[default]";
+ }
+ else if (_.isUndefined(value) && columnDef.not_null) {
+ return ''; // If null value not allowed, set cell to blank
+ }
+ else if (_.isUndefined(value) || _.isNull(value)) {
return "[null]";
}
else {
diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py
index d114988..f7466d8 100644
--- a/web/pgadmin/tools/sqleditor/__init__.py
+++ b/web/pgadmin/tools/sqleditor/__init__.py
@@ -440,8 +440,23 @@ def get_columns(trans_id):
columns = dict()
columns_info = None
primary_keys = None
+ rset = None
status, error_msg, conn, trans_obj, session_obj = check_transaction_status(trans_id)
if status and conn is not None and session_obj is not None:
+
+ ver = conn.manager.version
+ # Get the template path for the column
+ template_path = 'column/sql/#{0}#'.format(ver)
+ command_obj = pickle.loads(session_obj['command_obj'])
+ if hasattr(command_obj, 'obj_id'):
+ SQL = render_template("/".join([template_path,
+ 'nodes.sql']),
+ tid=command_obj.obj_id)
+ # rows with attribute not_null
+ status, rset = conn.execute_2darray(SQL)
+ if not status:
+ return internal_server_error(errormsg=rset)
+
# Check PK column info is available or not
if 'primary_keys' in session_obj:
primary_keys = session_obj['primary_keys']
@@ -449,10 +464,17 @@ def get_columns(trans_id):
# Fetch column information
columns_info = conn.get_column_info()
if columns_info is not None:
- for col in columns_info:
+ for key, col in enumerate(columns_info):
col_type = dict()
col_type['type_code'] = col['type_code']
col_type['type_name'] = None
+ if rset:
+ col_type['not_null'] = col['not_null'] = \
+ rset['rows'][key]['not_null']
+
+ col_type['has_default_val'] = col['has_default_val'] = \
+ rset['rows'][key]['has_default_val']
+
columns[col['name']] = col_type
# As we changed the transaction object we need to
@@ -602,6 +624,7 @@ def save(trans_id):
status, error_msg, conn, trans_obj, session_obj = check_transaction_status(trans_id)
if status and conn is not None \
and trans_obj is not None and session_obj is not None:
+ setattr(trans_obj, 'columns_info', session_obj['columns_info'])
# If there is no primary key found then return from the function.
if len(session_obj['primary_keys']) <= 0 or len(changed_data) <= 0:
diff --git a/web/pgadmin/tools/sqleditor/command.py b/web/pgadmin/tools/sqleditor/command.py
index be7f21f..a9ff617 100644
--- a/web/pgadmin/tools/sqleditor/command.py
+++ b/web/pgadmin/tools/sqleditor/command.py
@@ -442,6 +442,23 @@ class TableCommand(GridCommand):
# For newly added rows
if of_type == 'added':
+
+ # When new rows are added, only changed columns data is
+ # sent from client side. But if column is not_null and has
+ # no_default_value, set column to blank, instead
+ # of not null which is set by default.
+ column_data = {}
+ column_type = {}
+ for each_col in self.columns_info:
+ if (
+ self.columns_info[each_col]['not_null'] and
+ not self.columns_info[each_col][
+ 'has_default_val']
+ ):
+ column_data[each_col] = ""
+ column_type[each_col] =\
+ self.columns_info[each_col]['type_name']
+
for each_row in changed_data[of_type]:
data = changed_data[of_type][each_row]['data']
# Remove our unique tracking key
@@ -450,12 +467,18 @@ class TableCommand(GridCommand):
data_type = set_column_names(changed_data[of_type][each_row]['data_type'])
list_of_rowid.append(data.get('__temp_PK'))
+ # Update columns value and data type
+ # with columns having not_null=False and has
+ # no default value
+ column_data.update(data)
+ column_type.update(data_type)
+
sql = render_template("/".join([self.sql_path, 'insert.sql']),
- data_to_be_saved=data,
+ data_to_be_saved=column_data,
primary_keys=None,
object_name=self.object_name,
nsp_name=self.nsp_name,
- data_type=data_type)
+ data_type=column_type)
list_of_sql.append(sql)
# For updated rows
diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
index 2062aa2..ad9a565 100644
--- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
@@ -526,6 +526,13 @@ define(
render_grid: function(collection, columns, is_editable) {
var self = this;
+ var requiredFieldValidator = function (value) {
+ if (value == null || value == undefined || !value.length) {
+ return {valid: false, msg: "This is a required field"};
+ } else {
+ return {valid: true, msg: null};
+ }
+ }
// This will work as data store and holds all the
// inserted/updated/deleted data from grid
self.handler.data_store = {
@@ -557,7 +564,10 @@ define(
id: c.name,
pos: c.pos,
field: c.name,
- name: c.label
+ name: c.label,
+ not_null: c.not_null,
+ has_default_val: c.has_default_val,
+ validator: c.not_null ? requiredFieldValidator : null
};
// Get the columns width based on data type
@@ -2077,7 +2087,9 @@ define(
'label': column_label,
'cell': col_cell,
'can_edit': self.can_edit,
- 'type': type
+ 'type': type,
+ 'not_null': c.not_null,
+ 'has_default_val': c.has_default_val
};
columns.push(col);
});