์ด์ ๋ํ ์ ์ฝ ์กฐ๊ฑด์ ๋ณ๊ฒฝํ๋ ค๊ณ ํ์ง๋ง ์ด์ ๋ค์ ๋ง๋ค๋ ค๊ณ ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ณ ์คํจํฉ๋๋ค.
exports.up = function(knex, Promise) {
return knex.schema.table("images", function(table) {
table.string("checksum").notNullable();
});
};
exports.down = function(knex, Promise) {
return knex.schema.table("images", function(table) {
table.string("checksum").nullable();
});
};
@Rush ์ ๋ .nullable()
๋ฐ .notNullable()
๊ฐ ์ด๊ฒ์ ์ํด ์ฌ์ฉ๋์ด์ผ ํ๋ค๊ณ ์๊ฐํ์ง ์์ต๋๋ค. ๋๋ ๋น์ ์ด ๋ค์๊ณผ ๊ฐ์ ๊ฒ์ ์คํํ๊ณ ์ถ์ดํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
ALTER TABLE tableName ALTER COLUMN columnName DROP NOT NULL
ALTER TABLE tableName ALTER COLUMN columnName SET NOT NULL
๊ทธ๋ฆฌ๊ณ ํ์ฌ .raw
์ธ์๋ API์์ ์ด๊ฒ์ ์ฒ๋ฆฌํ ์ ์๋ ๊ฒ์ ์ฐพ์ ์ ์์ต๋๋ค.
@rhys-vdw ์ด์ ๋ํด ์ด๋ป๊ฒ ์๊ฐํ์ญ๋๊น? .dropNotNull()
/ .setNotNull()
๋๋ ์ด์ ์ ์ฌํ ๊ฒ์ด schemaBuilder์ ์ถ๊ฐํ ๊ฐ์น๊ฐ ์์ต๋๊น? .dropForeign()
์ ๊ตฌํ๊ณผ ๋งค์ฐ ์ ์ฌํฉ๋๋ค.
.dropNotNull()/.setNotNull() ๋๋ ์ด์ ์ ์ฌํ ๊ฒ์ด schemaBuilder์ ์ถ๊ฐํ ๊ฐ์น๊ฐ ์์ต๋๊น?
์, ํ์ง๋ง ์๋ง๋ .dropNullable(...columnNames)
๋ฐ .setNullable(...columnNames)
์
๋๋ค.
@rhys-vdw ๋ค์ ์๊ฐํด๋ณด๋ฉด ์ด๊ฒ์ด postgres ์ด์ธ์ ๋ค๋ฅธ ๋ฐฉ์ธ์๋ ๋๋ฌด ๋ถํธํ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ธ์ ์ผ๋ก ๋๋ ๋ค์์ ๋ํ ์ข์ ํด๊ฒฐ์ฑ ์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
Postgres dropNullable: ALTER TABLE table ALTER COLUMN columnName DROP NOT NULL
Postgres setNullable: ALTER TABLE table ALTER COLUMN columnName SET NOT NULL
MSSQL dropNullable: ALTER TABLE table ALTER COLUMN columnName columnType NULL
MSSQL setNullable: ALTER TABLE table ALTER COLUMN columnName columnType NOT NULL
MySQL/Maria/Oracle dropNullable: ALTER TABLE table MODIFY columnName columnType NULL;
MySQL/Maria/Oracle setNullable: ALTER TABLE table MODIFY columnName columnType NOT NULL;
Sqlite: No support?
์คํ ์ค์ธ ๋ฐฉ์ธ์ ๋ฐ๋ผ ์ผ๊ด๋์ง ์์ ์ธ์๊ฐ ๋ฐ์ํฉ๋๋ค. Postgres๋ ์ด๋ฆ๋ง ์ํ๋ ๋ฐ๋ฉด ๋ค๋ฅธ Postgres๋ ์ด๋ฆ + ๋ฐ์ดํฐ ์ ํ(+ ๊ธฐ๋ณธ๊ฐ?)์ ์ํฉ๋๋ค.
๊ฐ์น๊ฐ ์๋ค๊ณ ์๊ฐํ์ญ๋๊น?
์ฐ๋ฆฌ๋ SQLite์ Postgres๋ฅผ ๋ชจ๋ ์ง์ํ์ง๋ง SQLite๋ ๋๋ฌด ๋ฌ๋ผ์ ์ด๋ค ์ถ์ํ๋ก๋ ์จ๊ธธ ์ ์์ต๋๋ค... ๋ฐ๋ผ์ ์์ธ๋ฅผ ๋์ง๋ ๊ฒ์ ์๋ง๋ ๊ทธ๋ฐ ๊ฒฝ์ฐ์ ์ณ์ ์ผ์ ๋๋ค. Rails์ ActiveRecord ๋ง์ด๊ทธ๋ ์ด์ ์ ์ ์ฝ ์กฐ๊ฑด ๋ณ๊ฒฝ์ ์ง์ํ๋ฏ๋ก ์๊ฐ์ ์ป์ ์ ์๋ ๊ณณ์ด ์๋๊น์?
์ด๊ฒ์ ๊ฒฐ๊ตญ ๊ตฌํ ๊ฐ๋ฅํ์ต๋๋ค. PR์ ํ์ต๋๋ค.
+1 -- ์ ๋ ๋ฐฉ๊ธ ์ด ๋ฌธ์ ๋ฅผ ์ ํ๋๋ฐ ์ผ์ข ์ ์ฐจ๋จ ์ฅ์น์ ๋๋ค. nullable์ด ์๋ ์ด์ ๋ํ ํํ์ ๋งคํํ๋ ๋ง์ด๊ทธ๋ ์ด์ ์ ์ํํด์ผ ํ ๋๋ง๋ค ์ด๊ฒ์ด ํ์ํฉ๋๋ค.
@morungos ๋ ์ค์ ๋ก ์ฐจ๋จ๊ธฐ๊ฐ ์๋๋๋ค. knex์ ๋ํด ํน์ API๊ฐ ์๋ ํญ๋ชฉ(๋ง์ด๊ทธ๋ ์ด์ ์ ์์ฑํ ๋ ๋ง์ ํญ๋ชฉ)์ ๋ํด ํญ์ knex.raw์ ๋๋กญํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ด pull ์์ฒญ์ด ์๋ฃ๋๋ฉด ์ด ๋ฌธ์ ๋ฅผ ๋ซ์ ์๋ ์์ต๋๋ค. https://github.com/tgrisser/knex/pull/1759
๋ค, ์ ๋ .raw๋ฅผ ๋ช ๋ฒ ์ฌ์ฉํ์ง๋ง ์ ์ฒด ๋ฌธ์ฅ์ผ๋ก ๋ฐ๋๋ฉด ๋ฌด๋ฆ ๋ง์ด๊ทธ๋ ์ด์ ์ ์ ์ฒด ์ฌ์ฉ์ ์๋ฌธ์ ๊ฐ๊ธฐ ์์ํฉ๋๋ค. ๊ฒฐ๊ตญ์๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค. null์ด ์๋ ์ด์ํ ๊ฐ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ํ ๋ค์ ๋งคํ ๊ท์น์ ์ ์ฉํ์ฌ ๊ฐ์ ์ ๋ฐ์ดํธํ ๋ค์ ์๋ณธ์ ์ญ์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ํนํ MySQL์์ ๋ฌด๊ฒฐ์ฑ์ ๊ฐํํ๊ณ ์ค๊ฐ ๋จ๊ณ์์๋ ๋ฌด๊ฒฐ์ฑ์ ์๊ตฌํ๋ ๋์ ์ต๊ด์ด๋ผ๋ฉด ๊ทธ๋ ๊ฒ ํ๊ธฐ๊ฐ ์ด๋ ค์ธ ์ ์์ต๋๋ค.
Google์์ ์ฌ๊ธฐ๋ก ์ค๋ ์ฌ๋๋ค์ ์ํด ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Nullable์ด ์๋ ๊ธฐ์กด ์ด์ nullable๋ก ๋ณํ(Nullable์ด ์๋ ๊ฒ์ nullable๋ก ์ค์ ํ๋ ค๋ฉด ์/์๋๋ฅผ ๋ฐ๋๋ก).
exports.up = knex => {
return knex.schema
.alterTable('images', (table) => {
table.string('checksum').nullable().alter();
});
};
exports.down = knex => {
return knex.schema
.alterTable('images', table => {
table.string('checksum').notNullable().alter();
});
};
์ฐธ๊ณ ๋ก nullable
๋ฅผ nonNullable
๋ก ๋ณ๊ฒฝํ๋ ค๋ ๊ฒฝ์ฐ ๋จผ์ ํด๋น ์ด์ ๋ฌด์ธ๊ฐ๋ก ์ฑ์์ผ ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Google์์ ์ฌ๊ธฐ๋ก ์ค๋ ์ฌ๋๋ค์ ์ํด ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Nullable์ด ์๋ ๊ธฐ์กด ์ด์ nullable๋ก ๋ณํ(Nullable์ด ์๋ ๊ฒ์ nullable๋ก ์ค์ ํ๋ ค๋ฉด ์/์๋๋ฅผ ๋ฐ๋๋ก).
exports.up = knex => { return knex.schema .alterTable('images', (table) => { table.string('checksum').nullable().alter(); }); }; exports.down = knex => { return knex.schema .alterTable('images', table => { table.string('checksum').notNullable().alter(); }); };
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
Google์์ ์ฌ๊ธฐ๋ก ์ค๋ ์ฌ๋๋ค์ ์ํด ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Nullable์ด ์๋ ๊ธฐ์กด ์ด์ nullable๋ก ๋ณํ(Nullable์ด ์๋ ๊ฒ์ nullable๋ก ์ค์ ํ๋ ค๋ฉด ์/์๋๋ฅผ ๋ฐ๋๋ก).