์๋ฅผ ๋ค๋ฉด
ALTER TABLE users ADD UNIQUE KEY(LoginID, Email);
ALTER TABLE users ADD UNIQUE KEY(Email);
ALTER TABLE users ADD UNIQUE KEY(LoginID);
Knex.Schema.createTable
์ด๋ป๊ฒํ๋์?
๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ด์ผํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
Knex.Schema.createTable('tableName', function(table) {
table.unique(['LoginID', 'Email']);
table.unique('LoginID');
table.unique('Email');
});
์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ผ๋ฉด ์๋ ค์ฃผ์ธ์.
์ํ, ๊ฐ์ฌํฉ๋๋ค! Sequelize์์ ์ ํ ์ค๋น ์ค์ ๋๋ค.
๋๋ ๋ง์ด๊ทธ๋ ์ด์ ์ ์๋ํ๋ค
knex.schema.createTable('users', function (table) {
table.increments('id').primary();
table.string('network', 3).notNullable();
table.string('uid', 64).notNullable();
table.unique(['network', 'uid']);
table.timestamps();
}).then(function () {
console.log('Users Table is Created!');
});
ํ์ง๋ง ๊ณ ์ ์ธ๋ฑ์ค๋ฅผ ์ป์ง ๋ชปํฉ๋๋ค
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`network` varchar(3) NOT NULL,
`uid` varchar(64) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
๋ฌด์์ด ์๋ชป ๋ ์ ์์ต๋๊น?
@ theo-ptz API๊ฐ ์ผ๋ง ์ ์ ๋ณ๊ฒฝ๋์์ต๋๋ค. ๋ฌธ์์ ๋ฐ๋ผ unique
๋ ์ด์ primary
๋ฐ notNullable
์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฌ์ฉํ๋ ์ฒด์ธ ๊ฐ๋ฅํ ๋ฉ์๋์
๋๋ค.
@bendrucker , ๊ฐ์ฌํฉ๋๋ค.
๊ทธ๋ฌ๋ ๋ญ๊ฐ ์๋ํ์ง ์์ต๋๋ค.
์ด ์ฝ๋๋ฅผ ์คํํ๋ ค๊ณ
knex.schema.createTable('users', function (table) {
table.increments('id').primary();
table.string('network', 3).notNullable();
table.string('uid', 64).unique();
table.timestamps();
})
๋๋ ์ด๊ฒ์ ๋๋ฒ๊ทธ์์ ๋ณด์๋ค
--> ComQueryPacket
{ command: 3,
sql: 'create table
์ฌ์ฉ์ (
id int(11) unsigned not null auto_increment primary key,
๋คํธ์ํฌ varchar(3) not null,
uid varchar(64),
created_at datetime,
updated_at datetime) default character set utf8' }
์์ธ์ ์ป์ง ๋ชปํ์ต๋๋ค.
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`network` varchar(3) NOT NULL,
`uid` varchar(64) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
๋ฌด์์ด ์๋ชป ๋ ์ ์์ต๋๊น?
์ด์จ๋ ๋ง์ด๊ทธ๋ ์ด์ ์์ ๋ณตํฉ ๊ณ ์ ์ธ๋ฑ์ค๋ฅผ ๋ง๋ค ์ ์์ต๋๊น?
๋ณตํฉ ๊ณ ์ ์ ์ฝ ์กฐ๊ฑด์ ๋ํ ์์ ๋ ํตํฉ ํ ์คํธ๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
https://github.com/tgriesser/knex/blob/master/test/integration/schema/index.js#L92
@bendrucker ์์ค ํ์ผ์ ์ฐ๊ฒฐํ ๋ ๋ง์คํฐ ๋์ ํ๊ทธ / ์ปค๋ฐ์ ์ฌ์ฉํ์ธ์.
๋ ์ด์ ์๋ํ์ง ์๋ ๋งํฌ : https://github.com/tgriesser/knex/blob/master/test/integration/schema/index.js#L92
VS ์๋ฏธ : https://github.com/tgriesser/knex/blob/0.5.11/test/integration/schema/index.js#L92
์ต์ ๋ฒ์ (0.8.6) ์ฌ์ฉ : https://github.com/tgriesser/knex/blob/0.8.6/test/integration/schema/index.js#L157
Protip : github์์ ํ์ํ๋ ๋์ "y"ํค๋ฅผ ๋๋ฅด๋ฉด ํ์ฌ๋ณด๊ณ ์๋ ํ์ผ์ ํ์ฌ sha url๋ก ์ ํ๋ฉ๋๋ค. ๋งค์ฐ ํธ๋ฆฌํฉ๋๋ค. ๋๊ธ์ gh url์ ๋ถ์ฌ ๋ฃ์ ๋ github๊ฐ ์๋์ผ๋ก ์ํํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
์ด ๊ธฐ๋ฅ์ ๋ฌธ์์ ์๊ธฐ ๋๋ฌธ์ ๋ ์ด์ ์ฌ์ฉ๋์ง ์์ต๋๊น?
@RichardSimko ํ
์คํธ ์ค์ํธ์ ๋ฐ๋ฅด๋ฉด ๋ณตํฉ ํค์ ๋ํ table.unique([...])
๊ตฌ๋ฌธ์ ์ฌ์ ํ โโ์ง์๋๋ฏ๋ก ๋ฌธ์์ ๋ฒ๊ทธ์ฒ๋ผ ๋ณด์
๋๋ค.
๊ทธ๋, ๋๋ ๊ทธ๊ฒ์ด ์ฌ์ ํ ์๋ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์๊ณ , ๋ฌธ์์ ์์ง ์๊ธฐ ๋๋ฌธ์ ์ ๊ฑฐ ๋ ๊ฒ์ด๋ผ๊ณ ๊ฑฑ์ ํ์ต๋๋ค.
๊ทธ๋ ๋ค๋ฉด table.unique([...])
์ table.primary([...])
์ ์ฐจ์ด์ ์ ๋ฌด์์
๋๊น?
๋ฌธ์์ ๋ฐ๋ฅด๋ฉด primary([...])
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ณตํฉ ๊ธฐ๋ณธ ํค๋ฅผ ์ํํ๋ ์ ์ ํ ๋ฐฉ๋ฒ์
๋๋ค.
๋จ์ผ ์ด์์ ํธ์ถ๋๋ฉด ํด๋น ์ด์ ํ ์ด๋ธ์ ๊ธฐ๋ณธ ํค๋ก ์ค์ ํฉ๋๋ค. ๋ณตํฉ ๊ธฐ๋ณธ ํค๋ฅผ ์์ฑํ๋ ค๋ฉด ์ด ์ด๋ฆ ๋ฐฐ์ด์ ์ ๋ฌํฉ๋๋ค :
table.primary(['column1', 'column2'])
. ์ ์ ์ฝ ์ด๋ฆ์ ๊ธฐ๋ณธ๊ฐ์tablename_pkey
ํ์ง ์๋ ํconstraintName
์ง์ ๋ฉ๋๋ค.
http://knexjs.org/#Schema -primary
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ด์ผํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ผ๋ฉด ์๋ ค์ฃผ์ธ์.