You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sync() with "alter:true" doesn't use field name in model definition #9515
Your example seems incorrect as you are assuming test_key to be the column name, but it's the field attribute that determines the column name, so Sequelize is generating correct column name as testKey, as field specifies it.
Study the example given below. Notice in both cases table is created with user_email as it should be. Tested with latest v5 beta
constUser=sequelize.define('user',{email: {// `email` is the name used in model, at Node runtime levelfield: 'user_email',// `user_email` will be the column name, stored at database leveltype: Sequelize.STRING}});console.log('------- ALTER QUERIES START ---------');sequelize.sync({force: true,alter: true,logging: console.log}).then(()=>{console.log('------- ALTER QUERIES END ---------');console.log('------- SYNC QUERIES START ---------');returnsequelize.sync({force: true,logging: console.log});}).then(()=>{console.log('------- SYNC QUERIES END ---------');});
------- ALTER QUERIES START ---------
Executing (default): DROPTABLEIF EXISTS "users" CASCADE;
Executing (default): DROPTABLEIF EXISTS "users" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "users" ("id"SERIAL , "user_email"VARCHAR(255), "createdAt"TIMESTAMP WITH TIME ZONENOT NULL, "updatedAt"TIMESTAMP WITH TIME ZONENOT NULL, PRIMARY KEY ("id"));
Executing (default): ALTER TABLE "public"."users" DROP COLUMN "user_email";
Executing (default): SELECTi.relnameAS name, ix.indisprimaryAS primary, ix.indisuniqueAS unique, ix.indkeyAS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHEREt.oid=ix.indrelidANDi.oid=ix.indexrelidANDa.attrelid=t.oidANDt.relkind='r'andt.relname='users'GROUP BYi.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkeyORDER BYi.relname;
------- ALTER QUERIES END ---------------- SYNC QUERIES START ---------
Executing (default): DROPTABLEIF EXISTS "users" CASCADE;
Executing (default): DROPTABLEIF EXISTS "users" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "users" ("id"SERIAL , "user_email"VARCHAR(255), "createdAt"TIMESTAMP WITH TIME ZONENOT NULL, "updatedAt"TIMESTAMP WITH TIME ZONENOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECTi.relnameAS name, ix.indisprimaryAS primary, ix.indisuniqueAS unique, ix.indkeyAS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHEREt.oid=ix.indrelidANDi.oid=ix.indexrelidANDa.attrelid=t.oidANDt.relkind='r'andt.relname='users'GROUP BYi.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkeyORDER BYi.relname;
------- SYNC QUERIES END ---------
It try to find attribute by column name, which will fail when attribute and column name differs, should be easy to fix, with this.fieldRawAttributesMap
Activity
sushantdhiman commentedon Jun 8, 2018
Your example seems incorrect as you are assuming
test_keyto be the column name, but it's thefieldattribute that determines the column name, so Sequelize is generating correct column name astestKey, as field specifies it.Study the example given below. Notice in both cases table is created with
user_emailas it should be. Tested with latest v5 betasushantdhiman commentedon Jun 8, 2018
But I notice there is another, issue any attribute with
fieldis being dropped, inalter: truemodeSo we have a bug but different one
sushantdhiman commentedon Jun 8, 2018
Issue is appearing because of this line
sequelize/lib/model.js
Line 1254 in 9b1ca37
It try to find attribute by column name, which will fail when attribute and column name differs, should be easy to fix, with
this.fieldRawAttributesMapWill accept PRs
Fix sequelize#9515 alter mode uses attribute instead of column name, …
Fix sequelize#9515 (part 2) alter mode uses attribute instead of colu…