drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: drizzle-kit push:mysql throws error with only connectionString

Open zanzlender opened this issue 1 year ago • 2 comments

What version of drizzle-orm are you using?

0.26.1

What version of drizzle-kit are you using?

0.18.1

Describe the Bug

When I try to migrate (push) my schema to my DB it throws an error Error: Either connectionStringorhost, port, etc. params be provided in config file.

drizzle.config.ts

import type { Config } from "drizzle-kit";

export default {
  schema: "./db/schema/*",
  out: "./db/drizzle",
  connectionString: process.env["DATABASE_URL"],
} satisfies Config;
/db/index.ts

import { drizzle } from "drizzle-orm/planetscale-serverless";
import { connect } from "@planetscale/database";

// create the connection
const connection = connect({
  url: process.env["DATABASE_URL"],
});

export const db = drizzle(connection);

However I specified the connectionString... Am I doing something wrong here?

Expected behavior

It should push to db if I declared connectionString

Environment & setup

No response

zanzlender avatar May 29 '23 11:05 zanzlender

Ok, I just realized this was due to me/NextJS...

Since I was just running drizzle-kit push:mysql it didn't load env variable.... When replacing that with the actual string it works 👍


However, does anyone know how this could be fixed to work with .env normally?

zanzlender avatar May 29 '23 11:05 zanzlender

Accessing envs depends on your environment. In my case running anything from within my app (SvelteKit) get access to env via the special helper $env in SvelteKit. When I run migrations outside of my app via a local node script, it's process.env.SOME_VAR. So you need to check in which env (app-wise but also if local or remote/production) your script is really running and then, google how to get the env vars there. So, it's often more complicated than expected, mind to close that issue since it's not one with drizzle?

205g0 avatar May 31 '23 10:05 205g0

Ok, I just realized this was due to me/NextJS...

Since I was just running drizzle-kit push:mysql it didn't load env variable.... When replacing that with the actual string it works 👍

However, does anyone know how this could be fixed to work with .env normally?

I had this issue as well until I realized the same thing, the env variable was not being loaded.

Assuming your drizzle.config.ts file is located in the root directory like mine is, the way I was able to get the environment variable to load was by using the dotenv library. I discovered this by looking at this example from the docs, where they are also using dotenv to load the variable while having their drizzle.config.ts file in the root directory.

Hope this helps!

EthanHogan avatar Jun 11 '23 23:06 EthanHogan

Hi all,

If you're using NextJS, don't forget to change the path in dotenv.config({ path: 'your-env-path' }). It took me a while before I realised I had the variable stored in .env.local so I had to change my drizzle.config.ts to:

import type { Config } from 'drizzle-kit';
import * as dotenv from 'dotenv';

dotenv.config({
  path: '.env.local',
});

export default {
  schema: './src/lib/clients/planetscale/schema.ts',
  out: './src/lib/clients/planetscale/migrations',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

Additionally; you could create a 'normal' .env file and put your credentials there and dotenv would find them automatically as that's it's default path.

tmrp avatar Jun 27 '23 14:06 tmrp

if you do not want to use dotenv package in Nextjs then use @next/env:

import { cwd } from 'node:process';
import type { Config } from 'drizzle-kit';
import { loadEnvConfig } from '@next/env';

loadEnvConfig(cwd());

export default {
  schema: './src/db/schema.ts',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

shmshd avatar Aug 17 '23 06:08 shmshd

if you do not want to use dotenv package in Nextjs then use @next/env:

import { cwd } from 'node:process';
import type { Config } from 'drizzle-kit';
import { loadEnvConfig } from '@next/env';

loadEnvConfig(cwd());

export default {
  schema: './src/db/schema.ts',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

@shmshd thanks! Works like a charm and there is no need to install additional packages like dotenv

wojtekKrol avatar Aug 26 '23 10:08 wojtekKrol

someone found a solution for this on SvelteKit?

jesuscovam avatar Sep 11 '23 18:09 jesuscovam

I'm using Next version 13.4.19 and drizzle-kit 0.19.13 and is unable to access the studio.

Here is my config file :

import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config({ path: ".env" });

export default {
  driver: "pg",
  schema: "./src/lib/db/schema.ts",
  dbCredentials: {
    connectionString: process.env.DB_URL!,
  },
} satisfies Config;


While running npx drizzle-kit studio , it tells me that no config path is provided and reading config file from <FILE>/drizzle.config.ts.

The studio gets up and running but when I visit the URL, I am encountered with Problem Loading Page.

Not sure what happened wrong because all the packages are installed properly.

sanjaysanjel019 avatar Sep 15 '23 20:09 sanjaysanjel019

make sure your .env file called '.env' or just define the current name in dotenv.config:

import * as dotenv from "dotenv";
dotenv.config({ path: __dirname + "/.env.local" });

export default {
  driver: "pg",
  schema: "./lib/db/schema.ts",
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

maornissan avatar Sep 25 '23 21:09 maornissan

I'm using Next version 13.4.19 and drizzle-kit 0.19.13 and is unable to access the studio.

Here is my config file :

import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config({ path: ".env" });

export default {
  driver: "pg",
  schema: "./src/lib/db/schema.ts",
  dbCredentials: {
    connectionString: process.env.DB_URL!,
  },
} satisfies Config;

While running npx drizzle-kit studio , it tells me that no config path is provided and reading config file from /drizzle.config.ts.

The studio gets up and running but when I visit the URL, I am encountered with Problem Loading Page.

Not sure what happened wrong because all the packages are installed properly.

Hi, Did you solve it? I've tried all methods of this thread and none worked.

No config path provided, using default path Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

arb1ona avatar Oct 13 '23 19:10 arb1ona

I'm using Next version 13.4.19 and drizzle-kit 0.19.13 and is unable to access the studio. Here is my config file :

import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config({ path: ".env" });

export default {
  driver: "pg",
  schema: "./src/lib/db/schema.ts",
  dbCredentials: {
    connectionString: process.env.DB_URL!,
  },
} satisfies Config;

While running npx drizzle-kit studio , it tells me that no config path is provided and reading config file from /drizzle.config.ts. The studio gets up and running but when I visit the URL, I am encountered with Problem Loading Page. Not sure what happened wrong because all the packages are installed properly.

Hi, Did you solve it? I've tried all methods of this thread and none worked.

No config path provided, using default path Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

I was facing the same issue. The only problem was i copied this DATABASE_URL = postgres://username:@ep-misty-glitter-17983340.ap-southeast-1.aws.neon.tech/testdb?sslmode=require string from the neon dashboard but the password is missing here. we need to add the password as well. After adding password the string will look like this DATABASE_URL = postgres://username:4BIJdfd4gsWu@ep-misty-glitter-17983340.ap-southeast-1.aws.neon.tech/testdb?sslmode=true

sadiazaman-git avatar Nov 01 '23 15:11 sadiazaman-git

I'm using Next version 13.4.19 and drizzle-kit 0.19.13 and is unable to access the studio. Here is my config file :

import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config({ path: ".env" });

export default {
  driver: "pg",
  schema: "./src/lib/db/schema.ts",
  dbCredentials: {
    connectionString: process.env.DB_URL!,
  },
} satisfies Config;

While running npx drizzle-kit studio , it tells me that no config path is provided and reading config file from /drizzle.config.ts. The studio gets up and running but when I visit the URL, I am encountered with Problem Loading Page. Not sure what happened wrong because all the packages are installed properly.

Hi, Did you solve it? I've tried all methods of this thread and none worked.

No config path provided, using default path Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

My problem persist if I still have values in the .env file. When removing the values, I can see the .env.local are being used. Still not ideal for me..

rockgabi avatar Dec 09 '23 18:12 rockgabi

Hi @nukkerone,

change line dotenv.config({ path: ".env" ``});`

to

dotenv.config({ path: ".env.local" });

and check.

Check the schema path as well. "./src/lib/db/schema.ts",

JayanaGunaweera01 avatar Dec 20 '23 16:12 JayanaGunaweera01

Anyone having difficultly connecting with local mysql?

Like my string is: mysql://username:password@localhost:3306/masterdb

I can easily connect the same in terminal and tableplus but here it gives me this error:

code: 'ERR_INVALID_URL'

This came after I solved the error mentioned in this issue.

codedusting avatar Jan 31 '24 18:01 codedusting

ok, got same problem and just realize connectionString only exist in pg driver, i use mysql2 driver so i use uri

export default {
  schema: "./src/db/schema.js",
  out: './drizzle',
  driver: 'mysql2',
  dbCredentials: {
    uri: process.env.DB_URL
  }
}

isnakode avatar Feb 07 '24 13:02 isnakode

In case anyone else runs into this issue. I got the same No config path provided, using default 'drizzle.config.ts' error by using drizzle-kit generate:pg when I should have been using drizzle-kit generate:sqlite.

tylermorganme avatar Feb 14 '24 03:02 tylermorganme

Hi, i still have an error : "No config path provided, using default 'drizzle.config.ts" despite i followed all thread's recommandations ...

drizzle.config.ts

import * as dotenv from "dotenv";
import type { Config } from "drizzle-kit";

dotenv.config({ path: ".env" });

export default {
  schema: "./schema.ts",
  out: "./migrations",
  driver: "pg",
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

db.ts

import * as dotenv from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

dotenv.config({ path: ".env" });

const client = postgres(process.env.DATABASE_URL as string, { max: 1 });
export const db = drizzle(client, { schema });

all my files are in root directory folder and i'm using supabase as DB

Thanks for your help

ThomasBnf avatar Feb 16 '24 15:02 ThomasBnf

Hi, i still have an error : "No config path provided, using default 'drizzle.config.ts" despite i followed all thread's recommandations ...

drizzle.config.ts

import * as dotenv from "dotenv";
import type { Config } from "drizzle-kit";

dotenv.config({ path: ".env" });

export default {
  schema: "./schema.ts",
  out: "./migrations",
  driver: "pg",
  dbCredentials: {
    connectionString: process.env.DATABASE_URL as string,
  },
} satisfies Config;

db.ts

import * as dotenv from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

dotenv.config({ path: ".env" });

const client = postgres(process.env.DATABASE_URL as string, { max: 1 });
export const db = drizzle(client, { schema });

all my files are in root directory folder and i'm using supabase as DB

Thanks for your help

You can ignore this warning. Its just letting you know that you have not specified any path for drizzle config file and it's using the default config file it found on root directory.

iamtouha avatar Feb 23 '24 08:02 iamtouha

here is the way i solve the error, by using url ` import { defineConfig, type Config } from "drizzle-kit"; import * as dotenv from "dotenv"; dotenv.config({ path: ".env" });

export default defineConfig({ dialect: "postgresql", schema: "./api/database/schema", out: "./drizzle", driver: "pg", verbose: true, dbCredentials: { url: process.env.POSTGRES_URL!, }, } as Config); `

onlyhasbi avatar May 10 '24 15:05 onlyhasbi