Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add better support for unique columns #135

Merged

Conversation

WellingtonNico
Copy link
Contributor

Hello again!

I asked you how could I set a column as unique, you told me to use something like this:

index(ColumnIndex.unique, 'email', ['email']);

it didn't work because it generated this sql, which has a syntax error near INDEX(using postgres at least, but I checked in many AI models and both told me it is wrong for mysql too):

CREATE TABLE IF NOT EXISTS "users" (
    "id" SERIAL NOT NULL PRIMARY KEY,
    "name" VARCHAR(100) NOT NULL,
    "avatar" VARCHAR(250) NULL,
    "password" VARCHAR(200) NOT NULL,
    "email" VARCHAR(60) NOT NULL,
    "phone" VARCHAR(60) NULL,
    "description" TEXT NULL,
    "birthday" DATE NULL,
    "created_at" TIMESTAMP NULL,
    "updated_at" TIMESTAMP NULL,
    unique INDEX "email" ("email")
)

so I added an argument for each column type to generate unique columns with unique constraint, I tested for my migration and now it works, see the difference:
before:

await createTableNotExists('users', () {
      id();
      string('name', length: 100);
      string('avatar', length: 250, nullable: true);
      string('password', length: 200);
      string('email', length: 60);
      string('phone', length: 60, nullable: true);
      text('description', nullable: true);
      date('birthday', nullable: true);
      timeStamps();
      index(ColumnIndex.unique, 'email', ['email']);
});

after

await createTableNotExists('users', () {
      id();
      string('name', length: 100);
      string('avatar', length: 250, nullable: true);
      string('password', length: 200);
      string('email', length: 60,unique: true);
      string('phone', length: 60, nullable: true);
      text('description', nullable: true);
      date('birthday', nullable: true);
      timeStamps();
});

it now generates a query like this:

CREATE TABLE IF NOT EXISTS "users" (
    "id" SERIAL NOT NULL PRIMARY KEY,
    "name" VARCHAR(100) NOT NULL,
    "avatar" VARCHAR(250) NULL,
    "password" VARCHAR(200) NOT NULL,
    "email" VARCHAR(60) NOT NULL UNIQUE,
    "phone" VARCHAR(60) NULL,
    "description" TEXT NULL,
    "birthday" DATE NULL,
    "created_at" TIMESTAMP NULL,
    "updated_at" TIMESTAMP NULL
)

happy to help!

@WellingtonNico
Copy link
Contributor Author

any updates here guys?

@javad-zobeidi
Copy link
Collaborator

@WellingtonNico What if we have a combination of two columns?

ex:
combination mobile with email

@WellingtonNico
Copy link
Contributor Author

@WellingtonNico What if we have a combination of two columns?

ex: combination mobile with email

I think it's not a commom usage, thats way PRIMARY is made for...

@javad-zobeidi javad-zobeidi merged commit 2e8a0c0 into vania-dart:dev Oct 31, 2024
1 check passed
@javad-zobeidi
Copy link
Collaborator

@WellingtonNico
Thank you for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants