model.test.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. 'use strict';
  2. const afterLodash = require('lodash/after');
  3. const once = require('lodash/once');
  4. const chai = require('chai');
  5. const expect = chai.expect;
  6. const Support = require('./support');
  7. const { AggregateError, DataTypes, Op, Sequelize, sql } = require('@sequelize/core');
  8. const dialectName = Support.getTestDialect();
  9. const dialect = Support.sequelize.dialect;
  10. const sinon = require('sinon');
  11. const dayjs = require('dayjs');
  12. // ⚠️ Do not add tests to this file. Tests should be added to the new test suite in test/integration/model/<method-name>.ts
  13. describe(Support.getTestDialectTeaser('Model'), () => {
  14. before(function () {
  15. this.clock = sinon.useFakeTimers();
  16. });
  17. after(function () {
  18. this.clock.restore();
  19. });
  20. beforeEach(async function () {
  21. this.User = this.sequelize.define('User', {
  22. username: DataTypes.STRING,
  23. secretValue: DataTypes.STRING,
  24. data: DataTypes.STRING,
  25. intVal: DataTypes.INTEGER,
  26. theDate: DataTypes.DATE,
  27. aBool: DataTypes.BOOLEAN,
  28. });
  29. await this.User.sync({ force: true });
  30. });
  31. describe('constructor', () => {
  32. it('uses the passed dao name as tablename if freezeTableName', function () {
  33. const User = this.sequelize.define('FrozenUser', {}, { freezeTableName: true });
  34. expect(User.tableName).to.equal('FrozenUser');
  35. });
  36. it('uses the pluralized dao name as tablename unless freezeTableName', function () {
  37. const User = this.sequelize.define('SuperUser', {}, { freezeTableName: false });
  38. expect(User.tableName).to.equal('SuperUsers');
  39. });
  40. it('uses checks to make sure dao factory is not leaking on multiple define', function () {
  41. this.sequelize.define('SuperUser', {}, { freezeTableName: false });
  42. const factorySize = this.sequelize.models.size;
  43. this.sequelize.define('SuperUser', {}, { freezeTableName: false });
  44. const factorySize2 = this.sequelize.models.size;
  45. expect(factorySize).to.equal(factorySize2);
  46. });
  47. it('allows us to predefine the ID column with our own specs', async function () {
  48. const User = this.sequelize.define('UserCol', {
  49. id: {
  50. type: DataTypes.STRING,
  51. defaultValue: 'User',
  52. primaryKey: true,
  53. },
  54. });
  55. await User.sync({ force: true });
  56. expect(await User.create({ id: 'My own ID!' })).to.have.property('id', 'My own ID!');
  57. });
  58. it('throws an error if a custom model-wide validation is not a function', function () {
  59. expect(() => {
  60. this.sequelize.define(
  61. 'Foo',
  62. {
  63. columnName: DataTypes.INTEGER,
  64. },
  65. {
  66. validate: {
  67. notFunction: 33,
  68. },
  69. },
  70. );
  71. }).to.throw(
  72. Error,
  73. 'Members of the validate option must be functions. Model: Foo, error with validate member notFunction',
  74. );
  75. });
  76. it('should allow me to set a default value for createdAt and updatedAt', async function () {
  77. const UserTable = this.sequelize.define(
  78. 'UserCol',
  79. {
  80. aNumber: DataTypes.INTEGER,
  81. createdAt: {
  82. defaultValue: dayjs('2012-01-01').toDate(),
  83. },
  84. updatedAt: {
  85. defaultValue: dayjs('2012-01-02').toDate(),
  86. },
  87. },
  88. { timestamps: true },
  89. );
  90. await UserTable.sync({ force: true });
  91. const user = await UserTable.create({ aNumber: 5 });
  92. await UserTable.bulkCreate([{ aNumber: 10 }, { aNumber: 12 }]);
  93. const users = await UserTable.findAll({ where: { aNumber: { [Op.gte]: 10 } } });
  94. expect(dayjs(user.createdAt).format('YYYY-MM-DD')).to.equal('2012-01-01');
  95. expect(dayjs(user.updatedAt).format('YYYY-MM-DD')).to.equal('2012-01-02');
  96. for (const u of users) {
  97. expect(dayjs(u.createdAt).format('YYYY-MM-DD')).to.equal('2012-01-01');
  98. expect(dayjs(u.updatedAt).format('YYYY-MM-DD')).to.equal('2012-01-02');
  99. }
  100. });
  101. it('should allow me to set a function as default value', async function () {
  102. const defaultFunction = sinon.stub().returns(5);
  103. const UserTable = this.sequelize.define(
  104. 'UserCol',
  105. {
  106. aNumber: {
  107. type: DataTypes.INTEGER,
  108. defaultValue: defaultFunction,
  109. },
  110. },
  111. { timestamps: true },
  112. );
  113. await UserTable.sync({ force: true });
  114. const user = await UserTable.create();
  115. const user2 = await UserTable.create();
  116. expect(user.aNumber).to.equal(5);
  117. expect(user2.aNumber).to.equal(5);
  118. expect(defaultFunction.callCount).to.equal(2);
  119. });
  120. it('should throw `TypeError` when value for updatedAt, createdAt, or deletedAt is neither string nor boolean', async function () {
  121. const modelName = 'UserCol';
  122. const attributes = { aNumber: DataTypes.INTEGER };
  123. expect(() => {
  124. this.sequelize.define(modelName, attributes, { timestamps: true, updatedAt: {} });
  125. }).to.throw(Error, 'Value for "updatedAt" option must be a string or a boolean, got object');
  126. expect(() => {
  127. this.sequelize.define(modelName, attributes, { timestamps: true, createdAt: 100 });
  128. }).to.throw(Error, 'Value for "createdAt" option must be a string or a boolean, got number');
  129. expect(() => {
  130. this.sequelize.define(modelName, attributes, { timestamps: true, deletedAt: () => {} });
  131. }).to.throw(
  132. Error,
  133. 'Value for "deletedAt" option must be a string or a boolean, got function',
  134. );
  135. });
  136. it('should allow me to use `true` as a value for updatedAt, createdAt, and deletedAt fields', async function () {
  137. const UserTable = this.sequelize.define(
  138. 'UserCol',
  139. {
  140. aNumber: DataTypes.INTEGER,
  141. },
  142. {
  143. timestamps: true,
  144. updatedAt: true,
  145. createdAt: true,
  146. deletedAt: true,
  147. paranoid: true,
  148. },
  149. );
  150. await UserTable.sync({ force: true });
  151. const user = await UserTable.create({ aNumber: 4 });
  152. expect(user.true).to.not.exist;
  153. expect(user.updatedAt).to.exist;
  154. expect(user.createdAt).to.exist;
  155. await user.destroy();
  156. await user.reload({ paranoid: false });
  157. expect(user.deletedAt).to.exist;
  158. });
  159. it('should allow me to override updatedAt, createdAt, and deletedAt fields', async function () {
  160. const UserTable = this.sequelize.define(
  161. 'UserCol',
  162. {
  163. aNumber: DataTypes.INTEGER,
  164. },
  165. {
  166. timestamps: true,
  167. updatedAt: 'updatedOn',
  168. createdAt: 'dateCreated',
  169. deletedAt: 'deletedAtThisTime',
  170. paranoid: true,
  171. },
  172. );
  173. await UserTable.sync({ force: true });
  174. const user = await UserTable.create({ aNumber: 4 });
  175. expect(user.updatedOn).to.exist;
  176. expect(user.dateCreated).to.exist;
  177. await user.destroy();
  178. await user.reload({ paranoid: false });
  179. expect(user.deletedAtThisTime).to.exist;
  180. });
  181. it('should allow me to disable some of the timestamp fields', async function () {
  182. const UpdatingUser = this.sequelize.define(
  183. 'UpdatingUser',
  184. {
  185. name: DataTypes.STRING,
  186. },
  187. {
  188. timestamps: true,
  189. updatedAt: false,
  190. createdAt: false,
  191. deletedAt: 'deletedAtThisTime',
  192. paranoid: true,
  193. },
  194. );
  195. await UpdatingUser.sync({ force: true });
  196. let user = await UpdatingUser.create({ name: 'heyo' });
  197. expect(user.createdAt).not.to.exist;
  198. expect(user.false).not.to.exist; // because, you know we might accidentally add a field named 'false'
  199. user.name = 'heho';
  200. user = await user.save();
  201. expect(user.updatedAt).not.to.exist;
  202. await user.destroy();
  203. await user.reload({ paranoid: false });
  204. expect(user.deletedAtThisTime).to.exist;
  205. });
  206. it('should work with both paranoid and underscored being true', async function () {
  207. const UserTable = this.sequelize.define(
  208. 'UserCol',
  209. {
  210. aNumber: DataTypes.INTEGER,
  211. },
  212. {
  213. paranoid: true,
  214. underscored: true,
  215. },
  216. );
  217. await UserTable.sync({ force: true });
  218. await UserTable.create({ aNumber: 30 });
  219. expect(await UserTable.count()).to.equal(1);
  220. });
  221. it('allows unique on column with field aliases', async function () {
  222. const User = this.sequelize.define('UserWithUniqueFieldAlias', {
  223. userName: { type: DataTypes.STRING, unique: 'user_name_unique', columnName: 'user_name' },
  224. });
  225. await User.sync({ force: true });
  226. const indexes = (await this.sequelize.queryInterface.showIndex(User.table)).filter(
  227. index => !index.primary,
  228. );
  229. expect(indexes).to.have.length(1);
  230. const index = indexes[0];
  231. expect(index.primary).to.equal(false);
  232. expect(index.unique).to.equal(true);
  233. expect(index.name).to.equal('user_name_unique');
  234. switch (dialectName) {
  235. case 'mariadb':
  236. case 'mysql': {
  237. expect(index.fields).to.deep.equal([
  238. { attribute: 'user_name', length: undefined, order: 'ASC' },
  239. ]);
  240. expect(index.type).to.equal('BTREE');
  241. break;
  242. }
  243. case 'postgres': {
  244. expect(index.fields).to.deep.equal([
  245. { attribute: 'user_name', collate: undefined, order: undefined, length: undefined },
  246. ]);
  247. break;
  248. }
  249. case 'db2':
  250. case 'mssql': {
  251. expect(index.fields).to.deep.equal([
  252. { attribute: 'user_name', collate: undefined, length: undefined, order: 'ASC' },
  253. ]);
  254. break;
  255. }
  256. case 'sqlite3':
  257. default: {
  258. expect(index.fields).to.deep.equal([
  259. { attribute: 'user_name', length: undefined, order: undefined },
  260. ]);
  261. break;
  262. }
  263. }
  264. });
  265. if (dialectName !== 'ibmi') {
  266. it('allows us to customize the error message for unique constraint', async function () {
  267. const User = this.sequelize.define('UserWithUniqueUsername', {
  268. username: {
  269. type: DataTypes.STRING,
  270. unique: { name: 'user_and_email', msg: 'User and email must be unique' },
  271. },
  272. email: { type: DataTypes.STRING, unique: 'user_and_email' },
  273. });
  274. await User.sync({ force: true });
  275. try {
  276. await Promise.all([
  277. User.create({ username: 'tobi', email: 'tobi@tobi.me' }),
  278. User.create({ username: 'tobi', email: 'tobi@tobi.me' }),
  279. ]);
  280. } catch (error) {
  281. if (!(error instanceof Sequelize.UniqueConstraintError)) {
  282. throw error;
  283. }
  284. expect(error.message).to.equal('User and email must be unique');
  285. }
  286. });
  287. // If you use migrations to create unique indexes that have explicit names and/or contain fields
  288. // that have underscore in their name. Then sequelize must use the index name to map the custom message to the error thrown from db.
  289. it('allows us to map the customized error message with unique constraint name', async function () {
  290. // Fake migration style index creation with explicit index definition
  291. let User = this.sequelize.define(
  292. 'UserWithUniqueUsername',
  293. {
  294. user_id: { type: DataTypes.INTEGER },
  295. email: { type: DataTypes.STRING },
  296. },
  297. {
  298. indexes: [
  299. {
  300. name: 'user_and_email_index',
  301. msg: 'User and email must be unique',
  302. unique: true,
  303. method: 'BTREE',
  304. fields: [
  305. 'user_id',
  306. {
  307. attribute: 'email',
  308. collate: dialectName === 'sqlite3' ? 'RTRIM' : 'en_US',
  309. order: 'DESC',
  310. length: 5,
  311. },
  312. ],
  313. },
  314. ],
  315. },
  316. );
  317. await User.sync({ force: true });
  318. // Redefine the model to use the index in database and override error message
  319. User = this.sequelize.define('UserWithUniqueUsername', {
  320. user_id: {
  321. type: DataTypes.INTEGER,
  322. unique: { name: 'user_and_email_index', msg: 'User and email must be unique' },
  323. },
  324. email: { type: DataTypes.STRING, unique: 'user_and_email_index' },
  325. });
  326. try {
  327. await Promise.all([
  328. User.create({ user_id: 1, email: 'tobi@tobi.me' }),
  329. User.create({ user_id: 1, email: 'tobi@tobi.me' }),
  330. ]);
  331. } catch (error) {
  332. if (!(error instanceof Sequelize.UniqueConstraintError)) {
  333. throw error;
  334. }
  335. expect(error.message).to.equal('User and email must be unique');
  336. }
  337. });
  338. }
  339. describe('descending indices (MySQL specific)', () => {
  340. if (dialectName !== 'mysql') {
  341. return;
  342. }
  343. it('complains about missing support for descending indexes', async function () {
  344. const indices = [
  345. {
  346. name: 'a_b_uniq',
  347. unique: true,
  348. method: 'BTREE',
  349. fields: [
  350. 'fieldB',
  351. {
  352. attribute: 'fieldA',
  353. collate: 'en_US',
  354. order: 'DESC',
  355. length: 5,
  356. },
  357. ],
  358. },
  359. ];
  360. this.sequelize.define(
  361. 'model',
  362. {
  363. fieldA: DataTypes.STRING,
  364. fieldB: DataTypes.INTEGER,
  365. fieldC: DataTypes.STRING,
  366. fieldD: DataTypes.STRING,
  367. },
  368. {
  369. indexes: indices,
  370. engine: 'MyISAM',
  371. },
  372. );
  373. try {
  374. await this.sequelize.sync();
  375. expect.fail();
  376. } catch (error) {
  377. expect(error.message).to.include(
  378. "The storage engine for the table doesn't support descending indexes",
  379. );
  380. }
  381. });
  382. it('works fine with InnoDB', async function () {
  383. const indices = [
  384. {
  385. name: 'a_b_uniq',
  386. unique: true,
  387. method: 'BTREE',
  388. fields: [
  389. 'fieldB',
  390. {
  391. attribute: 'fieldA',
  392. collate: 'en_US',
  393. order: 'DESC',
  394. length: 5,
  395. },
  396. ],
  397. },
  398. ];
  399. this.sequelize.define(
  400. 'model',
  401. {
  402. fieldA: DataTypes.STRING,
  403. fieldB: DataTypes.INTEGER,
  404. fieldC: DataTypes.STRING,
  405. fieldD: DataTypes.STRING,
  406. },
  407. {
  408. indexes: indices,
  409. engine: 'InnoDB',
  410. },
  411. );
  412. await this.sequelize.sync();
  413. });
  414. });
  415. it('should allow the user to specify indexes in options', async function () {
  416. const indices = [
  417. {
  418. name: 'a_b_uniq',
  419. unique: true,
  420. method: 'BTREE',
  421. fields: [
  422. 'fieldB',
  423. {
  424. attribute: 'fieldA',
  425. collate: dialectName === 'sqlite3' ? 'RTRIM' : 'en_US',
  426. order:
  427. dialectName === 'ibmi'
  428. ? ''
  429. : // MySQL doesn't support DESC indexes (will throw)
  430. // MariaDB doesn't support DESC indexes (will silently replace it with ASC)
  431. dialectName === 'mysql' || dialectName === 'mariadb'
  432. ? 'ASC'
  433. : `DESC`,
  434. length: 5,
  435. },
  436. ],
  437. },
  438. ];
  439. if (!['mssql', 'db2', 'ibmi'].includes(dialectName)) {
  440. indices.push(
  441. {
  442. type: 'FULLTEXT',
  443. fields: ['fieldC'],
  444. concurrently: true,
  445. },
  446. {
  447. type: 'FULLTEXT',
  448. fields: ['fieldD'],
  449. },
  450. );
  451. }
  452. const modelOptions = ['mariadb', 'mysql'].includes(dialectName)
  453. ? { indexes: indices, engine: 'MyISAM' }
  454. : { indexes: indices };
  455. const Model = this.sequelize.define(
  456. 'model',
  457. {
  458. fieldA: DataTypes.STRING,
  459. fieldB: DataTypes.INTEGER,
  460. fieldC: DataTypes.STRING,
  461. fieldD: DataTypes.STRING,
  462. },
  463. modelOptions,
  464. );
  465. await this.sequelize.sync();
  466. await this.sequelize.sync(); // The second call should not try to create the indices again
  467. const args = await this.sequelize.queryInterface.showIndex(Model.table);
  468. let primary;
  469. let idx1;
  470. let idx2;
  471. let idx3;
  472. switch (dialectName) {
  473. case 'sqlite3': {
  474. // PRAGMA index_info does not return the primary index
  475. idx1 = args[0];
  476. idx2 = args[1];
  477. expect(idx1.fields).to.deep.equal([
  478. { attribute: 'fieldB', length: undefined, order: undefined },
  479. { attribute: 'fieldA', length: undefined, order: undefined },
  480. ]);
  481. expect(idx2.fields).to.deep.equal([
  482. { attribute: 'fieldC', length: undefined, order: undefined },
  483. ]);
  484. break;
  485. }
  486. case 'db2': {
  487. idx1 = args[1];
  488. expect(idx1.fields).to.deep.equal([
  489. { attribute: 'fieldB', length: undefined, order: 'ASC', collate: undefined },
  490. { attribute: 'fieldA', length: undefined, order: 'DESC', collate: undefined },
  491. ]);
  492. break;
  493. }
  494. case 'ibmi': {
  495. idx1 = args[0];
  496. expect(idx1.fields).to.deep.equal([
  497. { attribute: 'fieldA', length: undefined, order: undefined, collate: undefined },
  498. { attribute: 'fieldB', length: undefined, order: undefined, collate: undefined },
  499. ]);
  500. break;
  501. }
  502. case 'mssql': {
  503. idx1 = args[0];
  504. expect(idx1.fields).to.deep.equal([
  505. { attribute: 'fieldB', length: undefined, order: 'ASC', collate: undefined },
  506. { attribute: 'fieldA', length: undefined, order: 'DESC', collate: undefined },
  507. ]);
  508. break;
  509. }
  510. case 'postgres': {
  511. // Postgres returns indexes in alphabetical order
  512. primary = args[2];
  513. idx1 = args[0];
  514. idx2 = args[1];
  515. idx3 = args[2];
  516. expect(idx1.fields).to.deep.equal([
  517. { attribute: 'fieldB', length: undefined, order: undefined, collate: undefined },
  518. { attribute: 'fieldA', length: undefined, order: 'DESC', collate: 'en_US' },
  519. ]);
  520. expect(idx2.fields).to.deep.equal([
  521. { attribute: 'fieldC', length: undefined, order: undefined, collate: undefined },
  522. ]);
  523. expect(idx3.fields).to.deep.equal([
  524. { attribute: 'fieldD', length: undefined, order: undefined, collate: undefined },
  525. ]);
  526. break;
  527. }
  528. default: {
  529. // And finally mysql returns the primary first, and then the rest in the order they were defined
  530. primary = args[0];
  531. idx1 = args[1];
  532. idx2 = args[2];
  533. expect(primary.primary).to.be.ok;
  534. expect(idx1.type).to.equal('BTREE');
  535. expect(idx2.type).to.equal('FULLTEXT');
  536. expect(idx1.fields).to.deep.equal([
  537. { attribute: 'fieldB', length: undefined, order: 'ASC' },
  538. // length is a bigint, which is why it's returned as a string
  539. {
  540. attribute: 'fieldA',
  541. length: '5',
  542. // mysql & mariadb don't support DESC indexes
  543. order: 'ASC',
  544. },
  545. ]);
  546. expect(idx2.fields).to.deep.equal([
  547. { attribute: 'fieldC', length: undefined, order: null },
  548. ]);
  549. }
  550. }
  551. expect(idx1.name).to.equal('a_b_uniq');
  552. expect(idx1.unique).to.be.ok;
  553. if (!['mssql', 'db2', 'ibmi'].includes(dialectName)) {
  554. expect(idx2.name).to.equal('models_field_c');
  555. expect(idx2.unique).not.to.be.ok;
  556. }
  557. });
  558. });
  559. describe('build', () => {
  560. it("doesn't create database entries", async function () {
  561. this.User.build({ username: 'John Wayne' });
  562. expect(await this.User.findAll()).to.have.length(0);
  563. });
  564. it('fills the objects with default values', function () {
  565. const Task = this.sequelize.define('TaskBuild', {
  566. title: { type: DataTypes.STRING, defaultValue: 'a task!' },
  567. foo: { type: DataTypes.INTEGER, defaultValue: 2 },
  568. bar: { type: DataTypes.DATE },
  569. foobar: { type: DataTypes.TEXT, defaultValue: 'asd' },
  570. flag: { type: DataTypes.BOOLEAN, defaultValue: false },
  571. });
  572. expect(Task.build().title).to.equal('a task!');
  573. expect(Task.build().foo).to.equal(2);
  574. expect(Task.build().bar).to.not.be.ok;
  575. expect(Task.build().foobar).to.equal('asd');
  576. expect(Task.build().flag).to.be.false;
  577. });
  578. it('fills the objects with default values', function () {
  579. const Task = this.sequelize.define(
  580. 'TaskBuild',
  581. {
  582. title: { type: DataTypes.STRING, defaultValue: 'a task!' },
  583. foo: { type: DataTypes.INTEGER, defaultValue: 2 },
  584. bar: { type: DataTypes.DATE },
  585. foobar: { type: DataTypes.TEXT, defaultValue: 'asd' },
  586. flag: { type: DataTypes.BOOLEAN, defaultValue: false },
  587. },
  588. { timestamps: false },
  589. );
  590. expect(Task.build().title).to.equal('a task!');
  591. expect(Task.build().foo).to.equal(2);
  592. expect(Task.build().bar).to.not.be.ok;
  593. expect(Task.build().foobar).to.equal('asd');
  594. expect(Task.build().flag).to.be.false;
  595. });
  596. it('attaches getter and setter methods from attribute definition', function () {
  597. const Product = this.sequelize.define('ProductWithSettersAndGetters1', {
  598. price: {
  599. type: DataTypes.INTEGER,
  600. get() {
  601. return `answer = ${this.getDataValue('price')}`;
  602. },
  603. set(v) {
  604. return this.setDataValue('price', v + 42);
  605. },
  606. },
  607. });
  608. expect(Product.build({ price: 42 }).price).to.equal('answer = 84');
  609. const p = Product.build({ price: 1 });
  610. expect(p.price).to.equal('answer = 43');
  611. p.price = 0;
  612. expect(p.price).to.equal('answer = 42');
  613. });
  614. describe('include', () => {
  615. it('should support basic includes', function () {
  616. const Product = this.sequelize.define('Product', {
  617. title: DataTypes.STRING,
  618. });
  619. const Tag = this.sequelize.define('Tag', {
  620. name: DataTypes.STRING,
  621. });
  622. const User = this.sequelize.define('User', {
  623. first_name: DataTypes.STRING,
  624. last_name: DataTypes.STRING,
  625. });
  626. Product.hasMany(Tag);
  627. Product.belongsTo(User);
  628. const product = Product.build(
  629. {
  630. id: 1,
  631. title: 'Chair',
  632. tags: [
  633. { id: 1, name: 'Alpha' },
  634. { id: 2, name: 'Beta' },
  635. ],
  636. user: {
  637. id: 1,
  638. first_name: 'Mick',
  639. last_name: 'Hansen',
  640. },
  641. },
  642. {
  643. include: [User, Tag],
  644. },
  645. );
  646. expect(product.tags).to.be.ok;
  647. expect(product.tags.length).to.equal(2);
  648. expect(product.tags[0]).to.be.instanceof(Tag);
  649. expect(product.user).to.be.ok;
  650. expect(product.user).to.be.instanceof(User);
  651. });
  652. it('should support includes with aliases', function () {
  653. const Product = this.sequelize.define('Product', {
  654. title: DataTypes.STRING,
  655. });
  656. const Tag = this.sequelize.define('Tag', {
  657. name: DataTypes.STRING,
  658. });
  659. const User = this.sequelize.define('User', {
  660. first_name: DataTypes.STRING,
  661. last_name: DataTypes.STRING,
  662. });
  663. Product.hasMany(Tag, { as: 'categories' });
  664. Product.belongsToMany(User, { as: 'followers', through: 'product_followers' });
  665. User.belongsToMany(Product, { as: 'following', through: 'product_followers' });
  666. const product = Product.build(
  667. {
  668. id: 1,
  669. title: 'Chair',
  670. categories: [
  671. { id: 1, name: 'Alpha' },
  672. { id: 2, name: 'Beta' },
  673. { id: 3, name: 'Charlie' },
  674. { id: 4, name: 'Delta' },
  675. ],
  676. followers: [
  677. {
  678. id: 1,
  679. first_name: 'Mick',
  680. last_name: 'Hansen',
  681. },
  682. {
  683. id: 2,
  684. first_name: 'Jan',
  685. last_name: 'Meier',
  686. },
  687. ],
  688. },
  689. {
  690. include: [
  691. { model: User, as: 'followers' },
  692. { model: Tag, as: 'categories' },
  693. ],
  694. },
  695. );
  696. expect(product.categories).to.be.ok;
  697. expect(product.categories.length).to.equal(4);
  698. expect(product.categories[0]).to.be.instanceof(Tag);
  699. expect(product.followers).to.be.ok;
  700. expect(product.followers.length).to.equal(2);
  701. expect(product.followers[0]).to.be.instanceof(User);
  702. });
  703. });
  704. });
  705. describe('restore', () => {
  706. it('rejects with an error if the model is not paranoid', async function () {
  707. await expect(this.User.restore({ where: { secretValue: '42' } })).to.be.rejectedWith(
  708. Error,
  709. 'Model is not paranoid',
  710. );
  711. });
  712. it('restores a previously deleted model', async function () {
  713. const ParanoidUser = this.sequelize.define(
  714. 'ParanoidUser',
  715. {
  716. username: DataTypes.STRING,
  717. secretValue: DataTypes.STRING,
  718. data: DataTypes.STRING,
  719. intVal: { type: DataTypes.INTEGER, defaultValue: 1 },
  720. },
  721. {
  722. paranoid: true,
  723. },
  724. );
  725. const data = [
  726. { username: 'Peter', secretValue: '42' },
  727. { username: 'Paul', secretValue: '43' },
  728. { username: 'Bob', secretValue: '44' },
  729. ];
  730. await ParanoidUser.sync({ force: true });
  731. await ParanoidUser.bulkCreate(data);
  732. await ParanoidUser.destroy({ where: { secretValue: '42' } });
  733. await ParanoidUser.restore({ where: { secretValue: '42' } });
  734. const user = await ParanoidUser.findOne({ where: { secretValue: '42' } });
  735. expect(user).to.be.ok;
  736. expect(user.username).to.equal('Peter');
  737. });
  738. });
  739. describe('equals', () => {
  740. it('correctly determines equality of objects', async function () {
  741. const user = await this.User.create({ username: 'hallo', data: 'welt' });
  742. expect(user.equals(user)).to.be.ok;
  743. });
  744. // sqlite3 can't handle multiple primary keys
  745. if (dialectName !== 'sqlite3') {
  746. it('correctly determines equality with multiple primary keys', async function () {
  747. const userKeys = this.sequelize.define('userkeys', {
  748. foo: { type: DataTypes.STRING, primaryKey: true },
  749. bar: { type: DataTypes.STRING, primaryKey: true },
  750. name: DataTypes.STRING,
  751. bio: DataTypes.TEXT,
  752. });
  753. await userKeys.sync({ force: true });
  754. const user = await userKeys.create({ foo: '1', bar: '2', name: 'hallo', bio: 'welt' });
  755. expect(user.equals(user)).to.be.ok;
  756. });
  757. }
  758. });
  759. // sqlite can't handle multiple primary keys
  760. if (dialectName !== 'sqlite3') {
  761. describe('equalsOneOf', () => {
  762. beforeEach(async function () {
  763. this.userKey = this.sequelize.define('userKeys', {
  764. foo: { type: DataTypes.STRING, primaryKey: true },
  765. bar: { type: DataTypes.STRING, primaryKey: true },
  766. name: DataTypes.STRING,
  767. bio: DataTypes.TEXT,
  768. });
  769. await this.userKey.sync({ force: true });
  770. });
  771. it('determines equality if one is matching', async function () {
  772. const u = await this.userKey.create({ foo: '1', bar: '2', name: 'hallo', bio: 'welt' });
  773. expect(u.equalsOneOf([u, { a: 1 }])).to.be.ok;
  774. });
  775. it("doesn't determine equality if none is matching", async function () {
  776. const u = await this.userKey.create({ foo: '1', bar: '2', name: 'hallo', bio: 'welt' });
  777. expect(u.equalsOneOf([{ b: 2 }, { a: 1 }])).to.not.be.ok;
  778. });
  779. });
  780. }
  781. describe('sum', () => {
  782. beforeEach(async function () {
  783. this.UserWithAge = this.sequelize.define('UserWithAge', {
  784. age: DataTypes.INTEGER,
  785. order: DataTypes.INTEGER,
  786. gender: DataTypes.ENUM('male', 'female'),
  787. });
  788. this.UserWithFields = this.sequelize.define('UserWithFields', {
  789. age: {
  790. type: DataTypes.INTEGER,
  791. columnName: 'user_age',
  792. },
  793. order: DataTypes.INTEGER,
  794. gender: {
  795. type: DataTypes.ENUM('male', 'female'),
  796. columnName: 'male_female',
  797. },
  798. });
  799. await Promise.all([
  800. this.UserWithAge.sync({ force: true }),
  801. this.UserWithFields.sync({ force: true }),
  802. ]);
  803. });
  804. it('should work in the simplest case', async function () {
  805. await this.UserWithAge.bulkCreate([{ age: 2 }, { age: 3 }]);
  806. expect(await this.UserWithAge.sum('age')).to.equal(5);
  807. });
  808. it('should work with fields named as an SQL reserved keyword', async function () {
  809. await this.UserWithAge.bulkCreate([
  810. { age: 2, order: 3 },
  811. { age: 3, order: 5 },
  812. ]);
  813. expect(await this.UserWithAge.sum('order')).to.equal(8);
  814. });
  815. if (dialect.supports.dataTypes.DECIMAL) {
  816. it('should allow decimals in sum', async function () {
  817. const UserWithDec = this.sequelize.define('UserWithDec', {
  818. value: DataTypes.DECIMAL(10, 3),
  819. });
  820. await UserWithDec.sync({ force: true });
  821. await UserWithDec.bulkCreate([{ value: 3.5 }, { value: 5.25 }]);
  822. expect(await UserWithDec.sum('value')).to.equal(8.75);
  823. });
  824. }
  825. it('should accept a where clause', async function () {
  826. const options = { where: { gender: 'male' } };
  827. await this.UserWithAge.bulkCreate([
  828. { age: 2, gender: 'male' },
  829. { age: 3, gender: 'female' },
  830. ]);
  831. expect(await this.UserWithAge.sum('age', options)).to.equal(2);
  832. });
  833. it('should accept a where clause with custom fields', async function () {
  834. const options = { where: { gender: 'male' } };
  835. await this.UserWithFields.bulkCreate([
  836. { age: 2, gender: 'male' },
  837. { age: 3, gender: 'female' },
  838. ]);
  839. expect(await this.UserWithFields.sum('age', options)).to.equal(2);
  840. });
  841. it('allows sql logging', async function () {
  842. let test = false;
  843. await this.UserWithAge.sum('age', {
  844. logging(sql) {
  845. test = true;
  846. expect(sql).to.exist;
  847. expect(sql.toUpperCase()).to.include('SELECT');
  848. },
  849. });
  850. expect(test).to.true;
  851. });
  852. });
  853. if (dialect.supports.schemas) {
  854. describe('schematic support', () => {
  855. beforeEach(async function () {
  856. this.UserPublic = this.sequelize.define('UserPublic', {
  857. age: DataTypes.INTEGER,
  858. });
  859. this.UserSpecial = this.sequelize.define('UserSpecial', {
  860. age: DataTypes.INTEGER,
  861. });
  862. await Support.dropTestSchemas(this.sequelize);
  863. await this.sequelize.createSchema('schema_test');
  864. await this.sequelize.createSchema('special');
  865. this.UserSpecialSync = await this.UserSpecial.withSchema('special').sync({ force: true });
  866. });
  867. it('should be able to drop with schemas', async function () {
  868. await this.UserSpecial.drop();
  869. });
  870. it('should describeTable using the default schema settings', async function () {
  871. const UserPublic = this.sequelize.define('Public', {
  872. username: DataTypes.STRING,
  873. });
  874. let test = 0;
  875. await UserPublic.sync({ force: true });
  876. await UserPublic.withSchema('special').sync({ force: true });
  877. let table = await this.sequelize.queryInterface.describeTable('Publics', {
  878. logging(sql) {
  879. if (dialectName === 'sqlite3' && sql.includes('TABLE_INFO')) {
  880. test++;
  881. expect(sql).to.not.contain('special');
  882. } else if (['mysql', 'mssql', 'mariadb', 'db2', 'ibmi'].includes(dialectName)) {
  883. test++;
  884. expect(sql).to.not.contain('special');
  885. }
  886. },
  887. });
  888. if (dialectName === 'postgres') {
  889. test++;
  890. expect(table.id.defaultValue).to.not.contain('special');
  891. }
  892. table = await this.sequelize.queryInterface.describeTable(
  893. { tableName: 'Publics', schema: 'special' },
  894. {
  895. logging(sql) {
  896. if (dialectName === 'sqlite3' && sql.includes('TABLE_INFO')) {
  897. test++;
  898. expect(sql).to.contain('special');
  899. } else if (['mysql', 'mssql', 'mariadb', 'db2', 'ibmi'].includes(dialectName)) {
  900. test++;
  901. expect(sql).to.contain('special');
  902. }
  903. },
  904. },
  905. );
  906. if (dialectName === 'postgres') {
  907. test++;
  908. expect(table.id.defaultValue).to.contain('special');
  909. }
  910. expect(test).to.equal(2);
  911. });
  912. it('should be able to reference a table with a schema set', async function () {
  913. const UserPub = this.sequelize.define(
  914. 'UserPub',
  915. {
  916. username: DataTypes.STRING,
  917. },
  918. { schema: 'prefix' },
  919. );
  920. const ItemPub = this.sequelize.define(
  921. 'ItemPub',
  922. {
  923. name: DataTypes.STRING,
  924. },
  925. { schema: 'prefix' },
  926. );
  927. UserPub.hasMany(ItemPub, { foreignKeyConstraints: true });
  928. await this.sequelize.queryInterface.createSchema('prefix');
  929. let test = false;
  930. await UserPub.sync({ force: true });
  931. await ItemPub.sync({
  932. force: true,
  933. logging: afterLodash(
  934. 2,
  935. once(sql => {
  936. test = true;
  937. switch (dialectName) {
  938. case 'postgres':
  939. case 'db2':
  940. case 'ibmi': {
  941. expect(sql).to.match(/REFERENCES\s+"prefix"\."UserPubs" \("id"\)/);
  942. break;
  943. }
  944. case 'mssql': {
  945. expect(sql).to.match(/REFERENCES\s+\[prefix]\.\[UserPubs] \(\[id]\)/);
  946. break;
  947. }
  948. case 'mysql':
  949. case 'mariadb': {
  950. expect(sql).to.match(/REFERENCES\s+`prefix`\.`UserPubs` \(`id`\)/);
  951. break;
  952. }
  953. default: {
  954. expect(sql).to.match(/REFERENCES\s+`prefix\.UserPubs` \(`id`\)/);
  955. }
  956. }
  957. }),
  958. ),
  959. });
  960. expect(test).to.be.true;
  961. });
  962. it('should be able to create and update records under any valid schematic', async function () {
  963. let logged = 0;
  964. const UserPublicSync = await this.UserPublic.sync({ force: true });
  965. await UserPublicSync.create(
  966. { age: 3 },
  967. {
  968. logging: UserPublic => {
  969. logged++;
  970. expect(this.UserSpecialSync.table).to.deep.equal({
  971. tableName: 'UserSpecials',
  972. schema: 'special',
  973. delimiter: '.',
  974. });
  975. switch (dialectName) {
  976. case 'postgres':
  977. case 'db2':
  978. case 'ibmi': {
  979. expect(UserPublic).to.include('INSERT INTO "UserPublics"');
  980. break;
  981. }
  982. case 'mssql': {
  983. expect(UserPublic).to.include('INSERT INTO [UserPublics]');
  984. break;
  985. }
  986. case 'mysql':
  987. case 'mariadb':
  988. default: {
  989. expect(UserPublic.indexOf('INSERT INTO `UserPublics`')).to.be.above(-1);
  990. break;
  991. }
  992. }
  993. },
  994. },
  995. );
  996. const UserSpecial = await this.UserSpecialSync.withSchema('special').create(
  997. { age: 3 },
  998. {
  999. logging(UserSpecial) {
  1000. logged++;
  1001. switch (dialectName) {
  1002. case 'postgres':
  1003. case 'db2':
  1004. case 'ibmi': {
  1005. expect(UserSpecial).to.include('INSERT INTO "special"."UserSpecials"');
  1006. break;
  1007. }
  1008. case 'sqlite3': {
  1009. expect(UserSpecial).to.include('INSERT INTO `special.UserSpecials`');
  1010. break;
  1011. }
  1012. case 'mssql': {
  1013. expect(UserSpecial).to.include('INSERT INTO [special].[UserSpecials]');
  1014. break;
  1015. }
  1016. case 'mysql':
  1017. case 'mariadb':
  1018. default: {
  1019. expect(UserSpecial).to.include('INSERT INTO `special`.`UserSpecials`');
  1020. break;
  1021. }
  1022. }
  1023. },
  1024. },
  1025. );
  1026. await UserSpecial.update(
  1027. { age: 5 },
  1028. {
  1029. logging(user) {
  1030. logged++;
  1031. switch (dialectName) {
  1032. case 'postgres':
  1033. case 'db2':
  1034. case 'ibmi': {
  1035. expect(user).to.include('UPDATE "special"."UserSpecials"');
  1036. break;
  1037. }
  1038. case 'mssql': {
  1039. expect(user).to.include('UPDATE [special].[UserSpecials]');
  1040. break;
  1041. }
  1042. case 'mysql':
  1043. case 'mariadb':
  1044. default: {
  1045. expect(user).to.include('UPDATE `special`.`UserSpecials`');
  1046. break;
  1047. }
  1048. }
  1049. },
  1050. },
  1051. );
  1052. expect(logged).to.equal(3);
  1053. });
  1054. });
  1055. } else {
  1056. describe('fake schematic support', () => {
  1057. it('should take schemaDelimiter into account if applicable', async function () {
  1058. let test = 0;
  1059. const UserSpecialUnderscore = this.sequelize.define(
  1060. 'UserSpecialUnderscore',
  1061. {
  1062. age: DataTypes.INTEGER,
  1063. },
  1064. { schema: 'hello', schemaDelimiter: '_' },
  1065. );
  1066. const UserSpecialDblUnderscore = this.sequelize.define('UserSpecialDblUnderscore', {
  1067. age: DataTypes.INTEGER,
  1068. });
  1069. const User = await UserSpecialUnderscore.sync({ force: true });
  1070. const DblUser = await UserSpecialDblUnderscore.withSchema({
  1071. schema: 'hello',
  1072. schemaDelimiter: '__',
  1073. }).sync({ force: true });
  1074. await DblUser.create(
  1075. { age: 3 },
  1076. {
  1077. logging(sql) {
  1078. test++;
  1079. expect(sql).to.exist;
  1080. expect(sql).to.include('INSERT INTO `hello__UserSpecialDblUnderscores`');
  1081. },
  1082. },
  1083. );
  1084. await User.create(
  1085. { age: 3 },
  1086. {
  1087. logging(sql) {
  1088. test++;
  1089. expect(sql).to.exist;
  1090. expect(sql).to.include('INSERT INTO `hello_UserSpecialUnderscores`');
  1091. },
  1092. },
  1093. );
  1094. expect(test).to.equal(2);
  1095. });
  1096. });
  1097. }
  1098. describe('references', () => {
  1099. beforeEach(async function () {
  1100. this.Author = this.sequelize.define('author', { firstName: DataTypes.STRING });
  1101. await this.sequelize.queryInterface.dropTable('posts', { force: true });
  1102. await this.sequelize.queryInterface.dropTable('authors', { force: true });
  1103. await this.Author.sync();
  1104. });
  1105. it('uses an existing dao factory and references the author table', async function () {
  1106. const authorIdColumn = {
  1107. type: DataTypes.INTEGER,
  1108. references: { model: this.Author, key: 'id' },
  1109. };
  1110. const Post = this.sequelize.define('post', {
  1111. title: DataTypes.STRING,
  1112. authorId: authorIdColumn,
  1113. });
  1114. this.Author.hasMany(Post);
  1115. Post.belongsTo(this.Author);
  1116. // The posts table gets dropped in the before filter.
  1117. await Post.sync();
  1118. const foreignKeys = await this.sequelize.queryInterface.showConstraints(Post, {
  1119. constraintType: 'FOREIGN KEY',
  1120. });
  1121. expect(foreignKeys.length).to.eq(1);
  1122. expect(foreignKeys[0].columnNames).to.deep.eq(['authorId']);
  1123. expect(foreignKeys[0].referencedTableName).to.eq('authors');
  1124. expect(foreignKeys[0].referencedColumnNames).to.deep.eq(['id']);
  1125. });
  1126. it('uses a table name as a string and references the author table', async function () {
  1127. const authorIdColumn = {
  1128. type: DataTypes.INTEGER,
  1129. references: { table: 'authors', key: 'id' },
  1130. };
  1131. const Post = this.sequelize.define('post', {
  1132. title: DataTypes.STRING,
  1133. authorId: authorIdColumn,
  1134. });
  1135. this.Author.hasMany(Post);
  1136. Post.belongsTo(this.Author);
  1137. // The posts table gets dropped in the before filter.
  1138. await Post.sync();
  1139. const foreignKeys = await this.sequelize.queryInterface.showConstraints(Post, {
  1140. constraintType: 'FOREIGN KEY',
  1141. });
  1142. expect(foreignKeys.length).to.eq(1);
  1143. expect(foreignKeys[0].columnNames).to.deep.eq(['authorId']);
  1144. expect(foreignKeys[0].referencedTableName).to.eq('authors');
  1145. expect(foreignKeys[0].referencedColumnNames).to.deep.eq(['id']);
  1146. });
  1147. it('throws an error if the referenced table name is invalid', async function () {
  1148. const Post = this.sequelize.define('post', {
  1149. title: DataTypes.STRING,
  1150. authorId: DataTypes.INTEGER,
  1151. });
  1152. this.Author.hasMany(Post);
  1153. Post.belongsTo(this.Author);
  1154. // force Post.authorId to reference a table that does not exist
  1155. Post.modelDefinition.rawAttributes.authorId.references.table = '4uth0r5';
  1156. Post.modelDefinition.refreshAttributes();
  1157. try {
  1158. // The posts table gets dropped in the before filter.
  1159. await Post.sync();
  1160. if (dialectName === 'sqlite3') {
  1161. // sorry ... but sqlite is too stupid to understand whats going on ...
  1162. expect(1).to.equal(1);
  1163. } else {
  1164. // the parser should not end up here ...
  1165. expect(2).to.equal(1);
  1166. }
  1167. } catch (error) {
  1168. switch (dialectName) {
  1169. case 'mysql': {
  1170. expect(error.message).to.match(/Failed to open the referenced table '4uth0r5'/);
  1171. break;
  1172. }
  1173. case 'sqlite3': {
  1174. // the parser should not end up here ... see above
  1175. expect(1).to.equal(2);
  1176. break;
  1177. }
  1178. case 'mariadb': {
  1179. expect(error.message).to.match(/Foreign key constraint is incorrectly formed/);
  1180. break;
  1181. }
  1182. case 'postgres': {
  1183. expect(error.message).to.match(/relation "4uth0r5" does not exist/);
  1184. break;
  1185. }
  1186. case 'mssql': {
  1187. expect(error).to.be.instanceOf(AggregateError);
  1188. expect(error.errors.at(-2).message).to.match(/Could not create constraint/);
  1189. break;
  1190. }
  1191. case 'db2': {
  1192. expect(error.message).to.match(/ is an undefined name/);
  1193. break;
  1194. }
  1195. case 'ibmi': {
  1196. expect(error.message).to.match(
  1197. /[a-zA-Z0-9[\] /-]+?"4uth0r5" in SEQUELIZE type \*FILE not found\./,
  1198. );
  1199. break;
  1200. }
  1201. default: {
  1202. throw new Error('Undefined dialect!');
  1203. }
  1204. }
  1205. }
  1206. });
  1207. it('works with comments', async function () {
  1208. // Test for a case where the comment was being moved to the end of the table when there was also a reference on the column, see #1521
  1209. const Member = this.sequelize.define('Member', {});
  1210. const idColumn = {
  1211. type: DataTypes.INTEGER,
  1212. primaryKey: true,
  1213. autoIncrement: false,
  1214. comment: 'asdf',
  1215. };
  1216. idColumn.references = { model: Member, key: 'id' };
  1217. this.sequelize.define('Profile', { id: idColumn });
  1218. await this.sequelize.sync({ force: true });
  1219. });
  1220. });
  1221. describe('blob', () => {
  1222. beforeEach(async function () {
  1223. this.BlobUser = this.sequelize.define('blobUser', {
  1224. data: DataTypes.BLOB,
  1225. });
  1226. await this.BlobUser.sync({ force: true });
  1227. });
  1228. describe('buffers', () => {
  1229. it('should be able to take a buffer as parameter to a BLOB field', async function () {
  1230. const user = await this.BlobUser.create({
  1231. data: Buffer.from('Sequelize'),
  1232. });
  1233. expect(user).to.be.ok;
  1234. });
  1235. it('should return a buffer when fetching a blob', async function () {
  1236. const user = await this.BlobUser.create({
  1237. data: Buffer.from('Sequelize'),
  1238. });
  1239. const user0 = await this.BlobUser.findByPk(user.id);
  1240. expect(user0.data).to.be.an.instanceOf(Buffer);
  1241. expect(user0.data.toString()).to.have.string('Sequelize');
  1242. });
  1243. it('should work when the database returns null', async function () {
  1244. const user = await this.BlobUser.create({
  1245. // create a null column
  1246. });
  1247. const user0 = await this.BlobUser.findByPk(user.id);
  1248. expect(user0.data).to.be.null;
  1249. });
  1250. });
  1251. if (dialectName !== 'mssql') {
  1252. // NOTE: someone remember to inform me about the intent of these tests. Are
  1253. // you saying that data passed in as a string is automatically converted
  1254. // to binary? i.e. "Sequelize" is CAST as binary, OR that actual binary
  1255. // data is passed in, in string form? Very unclear, and very different.
  1256. describe('strings', () => {
  1257. it('should be able to take a string as parameter to a BLOB field', async function () {
  1258. const user = await this.BlobUser.create({
  1259. data: 'Sequelize',
  1260. });
  1261. expect(user).to.be.ok;
  1262. });
  1263. it('should return a buffer when fetching a BLOB, even when the BLOB was inserted as a string', async function () {
  1264. const user = await this.BlobUser.create({
  1265. data: 'Sequelize',
  1266. });
  1267. const user0 = await this.BlobUser.findByPk(user.id);
  1268. expect(user0.data).to.be.an.instanceOf(Buffer);
  1269. expect(user0.data.toString()).to.have.string('Sequelize');
  1270. });
  1271. });
  1272. }
  1273. });
  1274. describe('paranoid is true and where is an array', () => {
  1275. beforeEach(async function () {
  1276. this.User = this.sequelize.define('User', { username: DataTypes.STRING }, { paranoid: true });
  1277. this.Project = this.sequelize.define(
  1278. 'Project',
  1279. { title: DataTypes.STRING },
  1280. { paranoid: true },
  1281. );
  1282. this.Project.belongsToMany(this.User, { through: 'project_user' });
  1283. this.User.belongsToMany(this.Project, { through: 'project_user' });
  1284. await this.sequelize.sync({ force: true });
  1285. await this.User.bulkCreate([
  1286. {
  1287. username: 'leia',
  1288. },
  1289. {
  1290. username: 'luke',
  1291. },
  1292. {
  1293. username: 'vader',
  1294. },
  1295. ]);
  1296. await this.Project.bulkCreate([
  1297. {
  1298. title: 'republic',
  1299. },
  1300. {
  1301. title: 'empire',
  1302. },
  1303. ]);
  1304. const users = await this.User.findAll();
  1305. const projects = await this.Project.findAll();
  1306. const leia = users[0];
  1307. const luke = users[1];
  1308. const vader = users[2];
  1309. const republic = projects[0];
  1310. const empire = projects[1];
  1311. await leia.setProjects([republic]);
  1312. await luke.setProjects([republic]);
  1313. await vader.setProjects([empire]);
  1314. await leia.destroy();
  1315. });
  1316. it('should not fail when array contains Sequelize.or / and', async function () {
  1317. const res = await this.User.findAll({
  1318. where: [
  1319. this.sequelize.or({ username: 'vader' }, { username: 'luke' }),
  1320. this.sequelize.and({ id: [1, 2, 3] }),
  1321. ],
  1322. });
  1323. expect(res).to.have.length(2);
  1324. });
  1325. it('should not fail with an include', async function () {
  1326. const users = await this.User.findAll({
  1327. where: this.sequelize.literal(
  1328. `${this.sequelize.queryGenerator.quoteIdentifiers('projects.title')} = ${this.sequelize.queryGenerator.escape('republic')}`,
  1329. ),
  1330. include: [{ model: this.Project }],
  1331. });
  1332. expect(users.length).to.equal(1);
  1333. expect(users[0].username).to.equal('luke');
  1334. });
  1335. it('should not overwrite a specified deletedAt by setting paranoid: false', async function () {
  1336. let tableName = '';
  1337. if (this.User.name) {
  1338. tableName = `${this.sequelize.queryGenerator.quoteIdentifier(this.User.name)}.`;
  1339. }
  1340. const users = await this.User.findAll({
  1341. paranoid: false,
  1342. where: this.sequelize.literal(
  1343. `${tableName + this.sequelize.queryGenerator.quoteIdentifier('deletedAt')} IS NOT NULL `,
  1344. ),
  1345. include: [{ model: this.Project }],
  1346. });
  1347. expect(users.length).to.equal(1);
  1348. expect(users[0].username).to.equal('leia');
  1349. });
  1350. it('should not overwrite a specified deletedAt (complex query) by setting paranoid: false', async function () {
  1351. const res = await this.User.findAll({
  1352. paranoid: false,
  1353. where: [
  1354. this.sequelize.or({ username: 'leia' }, { username: 'luke' }),
  1355. this.sequelize.and(
  1356. { id: [1, 2, 3] },
  1357. this.sequelize.or({ deletedAt: null }, { deletedAt: { [Op.gte]: new Date(0) } }),
  1358. ),
  1359. ],
  1360. });
  1361. expect(res).to.have.length(2);
  1362. });
  1363. });
  1364. it('should be possible to use a key named UUID as foreign key', async function () {
  1365. this.sequelize.define('project', {
  1366. UserId: {
  1367. type: DataTypes.STRING,
  1368. references: {
  1369. tableName: 'Users',
  1370. key: 'UUID',
  1371. },
  1372. },
  1373. });
  1374. this.sequelize.define('Users', {
  1375. UUID: {
  1376. type: DataTypes.STRING,
  1377. primaryKey: true,
  1378. unique: true,
  1379. allowNull: false,
  1380. validate: {
  1381. notNull: true,
  1382. notEmpty: true,
  1383. },
  1384. },
  1385. });
  1386. await this.sequelize.sync({ force: true });
  1387. });
  1388. describe('bulkCreate', () => {
  1389. it('errors - should return array of errors if validate and individualHooks are true', async function () {
  1390. const data = [{ username: null }, { username: null }, { username: null }];
  1391. const user = this.sequelize.define('User', {
  1392. username: {
  1393. type: DataTypes.STRING,
  1394. allowNull: false,
  1395. validate: {
  1396. notNull: true,
  1397. notEmpty: true,
  1398. },
  1399. },
  1400. });
  1401. await this.sequelize.sync({ force: true });
  1402. expect(
  1403. user.bulkCreate(data, {
  1404. validate: true,
  1405. individualHooks: true,
  1406. }),
  1407. ).to.be.rejectedWith(AggregateError);
  1408. });
  1409. it('should not use setter when renaming fields in dataValues', async function () {
  1410. const user = this.sequelize.define('User', {
  1411. username: {
  1412. type: DataTypes.STRING,
  1413. allowNull: false,
  1414. columnName: 'data',
  1415. get() {
  1416. const val = this.getDataValue('username');
  1417. return val.slice(0, Math.max(0, val.length - 1));
  1418. },
  1419. set(val) {
  1420. if (val.includes('!')) {
  1421. throw new Error('val should not include a "!"');
  1422. }
  1423. this.setDataValue('username', `${val}!`);
  1424. },
  1425. },
  1426. });
  1427. const data = [{ username: 'jon' }];
  1428. await this.sequelize.sync({ force: true });
  1429. await user.bulkCreate(data);
  1430. const users1 = await user.findAll();
  1431. expect(users1[0].username).to.equal('jon');
  1432. });
  1433. it('should correctly set identifiers in a column with autoIncrement with bigint values', async function () {
  1434. // sqlite returns bigints as numbers https://github.com/sequelize/sequelize/issues/11400
  1435. if (dialectName === 'sqlite3') {
  1436. return;
  1437. }
  1438. const User = this.sequelize.define('User', {
  1439. id: {
  1440. type: DataTypes.BIGINT,
  1441. primaryKey: true,
  1442. autoIncrement: true,
  1443. },
  1444. username: DataTypes.STRING,
  1445. });
  1446. await this.sequelize.sync({ force: true });
  1447. await User.create({ id: '3415718944570971483', username: 'u1' });
  1448. const createdUsers = await User.bulkCreate([{ username: 'u2', id: '3415718944570971484' }]);
  1449. expect(createdUsers[0].id.toString()).to.equal('3415718944570971484');
  1450. const users1 = await User.findAll({ order: [['id', 'ASC']] });
  1451. expect(users1[0].username).to.equal('u1');
  1452. expect(users1[1].username).to.equal('u2');
  1453. expect(users1[1].id.toString()).to.equal('3415718944570971484');
  1454. });
  1455. });
  1456. describe('BulkUpdate', () => {
  1457. it('should update correctly when model defined has attributes with custom getters', async function () {
  1458. const User = this.sequelize.define('users', {
  1459. id: {
  1460. type: DataTypes.UUID,
  1461. primaryKey: true,
  1462. defaultValue: sql.uuidV4,
  1463. },
  1464. status: {
  1465. type: DataTypes.STRING,
  1466. defaultValue: 'active',
  1467. },
  1468. roles: {
  1469. type: DataTypes.STRING,
  1470. allowNull: false,
  1471. get() {
  1472. return this.getDataValue('roles').split(',');
  1473. },
  1474. set(val) {
  1475. this.setDataValue('roles', val.join(','));
  1476. },
  1477. },
  1478. });
  1479. await User.sync({ force: true });
  1480. const u1 = await User.create({
  1481. roles: ['authenticated user'],
  1482. });
  1483. const u2 = await User.create({
  1484. roles: ['authenticated user'],
  1485. });
  1486. await User.update(
  1487. { status: 'blocked' },
  1488. {
  1489. where: {
  1490. id: {
  1491. [Op.ne]: null,
  1492. },
  1493. },
  1494. },
  1495. );
  1496. const a1 = await User.findOne({ where: { id: u1.id } });
  1497. const a2 = await User.findOne({ where: { id: u2.id } });
  1498. expect(a1.get('status')).to.eq('blocked');
  1499. expect(a2.get('status')).to.eq('blocked');
  1500. });
  1501. });
  1502. });