不要怂,就是干,撸起袖子干!

Commit fe301789 by Jan Aagaard Meier

feat(describeTable) Detect primary key in describe table. Reroll of #3703

1 parent 538a095e
......@@ -7,6 +7,7 @@
- [FIXED] Calling `validateIncludedElements` should not add an aliassed primary key multiple times [#4127](https://github.com/sequelize/sequelize/issues/4127)
- [FEATURE] `addScope` [#3963](https://github.com/sequelize/sequelize/issues/3963)
- [FIXED] Handle scoped model in includes properly [#3700](https://github.com/sequelize/sequelize/issues/3700)
- [FEATURE] `describeTable` now marks the primary key (Reroll of [#3703](https://github.com/sequelize/sequelize/pull/3703))
# 3.4.1
- [FIXED] Fix belongs-to-many `countAssociations` - ambigious id when through model has id
......
......@@ -109,11 +109,16 @@ var QueryGenerator = {
"c.COLUMN_NAME AS 'Name',",
"c.DATA_TYPE AS 'Type',",
"c.IS_NULLABLE as 'IsNull',",
"COLUMN_DEFAULT AS 'Default'",
"COLUMN_DEFAULT AS 'Default',",
"tc.CONSTRAINT_TYPE AS 'Constraint'",
'FROM',
'INFORMATION_SCHEMA.TABLES t',
'INNER JOIN',
'INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_NAME = c.TABLE_NAME',
'LEFT JOIN',
'INFORMATION_SCHEMA.KEY_COLUMN_USAGE cu ON t.TABLE_NAME = cu.TABLE_NAME AND cu.COLUMN_NAME = c.COLUMN_NAME',
'LEFT JOIN',
'INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON t.TABLE_NAME = tc.TABLE_NAME AND cu.COLUMN_NAME = c.COLUMN_NAME AND tc.CONSTRAINT_TYPE = \'PRIMARY KEY\'',
'WHERE t.TABLE_NAME =', wrapSingleQuote(tableName)
].join(' ');
......
......@@ -125,13 +125,15 @@ Query.prototype.formatResults = function(data) {
} else if (this.isDescribeQuery()) {
result = {};
data.forEach(function(_result) {
if (_result.Default)
if (_result.Default) {
_result.Default = _result.Default.replace("('",'').replace("')",'').replace(/'/g,''); /* jshint ignore: line */
}
result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: (_result.IsNull === 'YES' ? true : false),
defaultValue: _result.Default
defaultValue: _result.Default,
primaryKey: _result.Constraint === 'PRIMARY KEY'
};
});
} else if (this.isShowIndexesQuery()) {
......
......@@ -80,7 +80,8 @@ Query.prototype.formatResults = function(data) {
result[_result.Field] = {
type: _result.Type.toUpperCase(),
allowNull: (_result.Null === 'YES'),
defaultValue: _result.Default
defaultValue: _result.Default,
primaryKey: _result.Key === 'PRI'
};
});
} else if (this.isShowIndexesQuery()) {
......
......@@ -103,11 +103,14 @@ var QueryGenerator = {
schema = 'public';
}
var query = 'SELECT c.column_name as "Field", c.column_default as "Default", c.is_nullable as "Null", ' +
var query = 'SELECT tc.constraint_type as "Constraint", c.column_name as "Field", c.column_default as "Default", c.is_nullable as "Null", ' +
"CASE WHEN c.udt_name = 'hstore' " +
'THEN c.udt_name ELSE c.data_type END as "Type", (SELECT array_agg(e.enumlabel) ' +
'FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special" ' +
'FROM information_schema.columns c WHERE table_name = <%= table %> AND table_schema = <%= schema %>';
'FROM information_schema.columns c ' +
'LEFT JOIN information_schema.key_column_usage cu ON c.table_name = cu.table_name AND cu.column_name = c.column_name ' +
'LEFT JOIN information_schema.table_constraints tc ON c.table_name = tc.table_name AND cu.column_name = c.column_name AND tc.constraint_type = \'PRIMARY KEY\' ' +
' WHERE c.table_name = <%= table %> AND c.table_schema = <%= schema %> ';
return Utils._.template(query)({
table: this.escape(tableName),
......
......@@ -244,7 +244,8 @@ Query.prototype.run = function(sql) {
type: _result.Type.toUpperCase(),
allowNull: (_result.Null === 'YES'),
defaultValue: _result.Default,
special: (!!_result.special ? self.sequelize.queryInterface.QueryGenerator.fromArray(_result.special) : [])
special: (!!_result.special ? self.sequelize.queryInterface.QueryGenerator.fromArray(_result.special) : []),
primaryKey: _result.Constraint === 'PRIMARY KEY'
};
if (result[_result.Field].type === 'BOOLEAN') {
......
......@@ -159,10 +159,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
expect(count).to.be.at.least(1);
count = 0;
var id = metadata.id;
var username = metadata.username;
var isAdmin = metadata.isAdmin;
var enumVals = metadata.enumVals;
expect(id.primaryKey).to.be.ok;
var assertVal = 'VARCHAR(255)';
switch (dialect) {
case 'postgres':
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!