create-table.test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const Support = require('../../support');
  3. const { DataTypes } = require('@sequelize/core');
  4. const expectsql = Support.expectsql;
  5. const current = Support.sequelize;
  6. const sql = current.dialect.queryGenerator;
  7. describe(Support.getTestDialectTeaser('SQL'), () => {
  8. if (current.dialect.name === 'snowflake') {
  9. return;
  10. }
  11. describe('createTable', () => {
  12. describe('with enums', () => {
  13. it('references enum in the right schema #3171', () => {
  14. const FooUser = current.define(
  15. 'user',
  16. {
  17. mood: DataTypes.ENUM('happy', 'sad'),
  18. },
  19. {
  20. schema: 'foo',
  21. timestamps: false,
  22. },
  23. );
  24. expectsql(
  25. sql.createTableQuery(FooUser.table, sql.attributesToSQL(FooUser.getAttributes()), {}),
  26. {
  27. sqlite3:
  28. 'CREATE TABLE IF NOT EXISTS `foo.users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `mood` TEXT);',
  29. db2: 'CREATE TABLE IF NOT EXISTS "foo"."users" ("id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) , "mood" VARCHAR(255) CHECK ("mood" IN(\'happy\', \'sad\')), PRIMARY KEY ("id"));',
  30. postgres:
  31. 'CREATE TABLE IF NOT EXISTS "foo"."users" ("id" SERIAL , "mood" "foo"."enum_users_mood", PRIMARY KEY ("id"));',
  32. 'mariadb mysql':
  33. "CREATE TABLE IF NOT EXISTS `foo`.`users` (`id` INTEGER NOT NULL auto_increment , `mood` ENUM('happy', 'sad'), PRIMARY KEY (`id`)) ENGINE=InnoDB;",
  34. mssql: `IF OBJECT_ID(N'[foo].[users]', 'U') IS NULL CREATE TABLE [foo].[users] ([id] INTEGER NOT NULL IDENTITY(1,1) , [mood] NVARCHAR(255) CHECK ([mood] IN(N'happy', N'sad')), PRIMARY KEY ([id]));`,
  35. ibmi: `BEGIN
  36. DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '42710'
  37. BEGIN END;
  38. CREATE TABLE "foo"."users" ("id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) , "mood" VARCHAR(255) CHECK ("mood" IN('happy', 'sad')), PRIMARY KEY ("id"));
  39. END`,
  40. },
  41. );
  42. });
  43. });
  44. describe('with references', () => {
  45. it('references right schema when adding foreign key #9029', () => {
  46. const BarUser = current.define('user', {}, { timestamps: false }).withSchema('bar');
  47. const BarProject = current
  48. .define(
  49. 'project',
  50. {
  51. user_id: {
  52. type: DataTypes.INTEGER,
  53. references: { model: BarUser },
  54. onUpdate: 'CASCADE',
  55. onDelete: 'NO ACTION',
  56. },
  57. },
  58. {
  59. timestamps: false,
  60. },
  61. )
  62. .withSchema('bar');
  63. BarProject.belongsTo(BarUser, { foreignKey: 'user_id' });
  64. expectsql(
  65. sql.createTableQuery(
  66. BarProject.table,
  67. sql.attributesToSQL(BarProject.getAttributes()),
  68. {},
  69. ),
  70. {
  71. sqlite3:
  72. 'CREATE TABLE IF NOT EXISTS `bar.projects` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_id` INTEGER REFERENCES `bar.users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE);',
  73. db2: 'CREATE TABLE IF NOT EXISTS "bar"."projects" ("id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) , "user_id" INTEGER, PRIMARY KEY ("id"), FOREIGN KEY ("user_id") REFERENCES "bar"."users" ("id") ON DELETE NO ACTION);',
  74. postgres:
  75. 'CREATE TABLE IF NOT EXISTS "bar"."projects" ("id" SERIAL , "user_id" INTEGER REFERENCES "bar"."users" ("id") ON DELETE NO ACTION ON UPDATE CASCADE, PRIMARY KEY ("id"));',
  76. 'mariadb mysql':
  77. 'CREATE TABLE IF NOT EXISTS `bar`.`projects` (`id` INTEGER NOT NULL auto_increment , `user_id` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `bar`.`users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;',
  78. mssql: `IF OBJECT_ID(N'[bar].[projects]', 'U') IS NULL CREATE TABLE [bar].[projects] ([id] INTEGER NOT NULL IDENTITY(1,1) , [user_id] INTEGER NULL, PRIMARY KEY ([id]), FOREIGN KEY ([user_id]) REFERENCES [bar].[users] ([id]) ON DELETE NO ACTION);`,
  79. ibmi: `BEGIN
  80. DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '42710'
  81. BEGIN END;
  82. CREATE TABLE "bar"."projects" ("id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) , "user_id" INTEGER REFERENCES "bar"."users" ("id") ON DELETE NO ACTION, PRIMARY KEY ("id"));
  83. END`,
  84. },
  85. );
  86. });
  87. });
  88. describe('with references on primary key', () => {
  89. it('references on primary key #9461', () => {
  90. const File = current.define('file', {}, { timestamps: false });
  91. const Image = current.define(
  92. 'image',
  93. {
  94. id: {
  95. primaryKey: true,
  96. autoIncrement: true,
  97. type: DataTypes.INTEGER,
  98. references: {
  99. model: File,
  100. key: 'id',
  101. },
  102. },
  103. },
  104. {
  105. timestamps: false,
  106. },
  107. );
  108. expectsql(
  109. sql.createTableQuery(Image.table, sql.attributesToSQL(Image.getAttributes()), {}),
  110. {
  111. sqlite3:
  112. 'CREATE TABLE IF NOT EXISTS `images` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `files` (`id`));',
  113. postgres:
  114. 'CREATE TABLE IF NOT EXISTS "images" ("id" SERIAL REFERENCES "files" ("id"), PRIMARY KEY ("id"));',
  115. db2: 'CREATE TABLE IF NOT EXISTS "images" ("id" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) , PRIMARY KEY ("id"), FOREIGN KEY ("id") REFERENCES "files" ("id"));',
  116. mariadb:
  117. 'CREATE TABLE IF NOT EXISTS `images` (`id` INTEGER auto_increment , PRIMARY KEY (`id`), FOREIGN KEY (`id`) REFERENCES `files` (`id`)) ENGINE=InnoDB;',
  118. mysql:
  119. 'CREATE TABLE IF NOT EXISTS `images` (`id` INTEGER auto_increment , PRIMARY KEY (`id`), FOREIGN KEY (`id`) REFERENCES `files` (`id`)) ENGINE=InnoDB;',
  120. mssql: `IF OBJECT_ID(N'[images]', 'U') IS NULL CREATE TABLE [images] ([id] INTEGER IDENTITY(1,1) , PRIMARY KEY ([id]), FOREIGN KEY ([id]) REFERENCES [files] ([id]));`,
  121. ibmi: `BEGIN
  122. DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '42710'
  123. BEGIN END;
  124. CREATE TABLE "images" ("id" INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) REFERENCES "files" ("id"), PRIMARY KEY ("id"));
  125. END`,
  126. },
  127. );
  128. });
  129. });
  130. });
  131. });