Is it way to apply unique by two columns constraint to model using peewee. There are raw way (using SQL()
) but I'm finding another.
Updated:
I'm using postgres so I tried to do like @booshong says below then in raw SQL I gave the following (for simplicity transaction related BEGIN
and COMMIT
omitted and output was intended):
CREATE TABLE IF NOT EXISTS "foo" (
"id" SERIAL NOT NULL PRIMARY KEY,
"field_1" VARCHAR(255) NOT NULL,
"field_2" VARCHAR(255) NOT NULL);
CREATE UNIQUE INDEX IF NOT EXISTS "foo_field_1_field_2"
ON "foo" ("field_1", "field_2");
CREATE TABLE IF NOT EXISTS "foo2" (
"id" SERIAL NOT NULL PRIMARY KEY,
"field_1" VARCHAR(255) NOT NULL,
"field_2" VARCHAR(255) NOT NULL,
UNIQUE (field_1, field_2));
And as we can see it's different things as I say earlier.