next-learn icon indicating copy to clipboard operation
next-learn copied to clipboard

Chapter 6: Cannot seed database

Open daweed311 opened this issue 1 year ago • 22 comments

Hi Team,

I trust this message finds you in good spirits. 🌟 Your commitment to the project is truly appreciated, and we welcome your active involvement.

We've identified an issue post-seeding the database. Although the storage isn't empty, there are discrepancies we need your expertise to address:

image

Description: After seeding the database, non-empty storage is observed.

However, when checking the tables list, it appears to be empty, and the connection status remains stuck at Connecting:

image

This discrepancy is hindering the expected behavior of the system. We would greatly appreciate your insights and contributions to resolve this issue.

Details:

Issue Title: Database Seeding Discrepancy Description: Non-empty storage, yet tables list is empty, and connection status is Connecting.

Thank you for your ongoing support!

daweed311 avatar Nov 25 '23 17:11 daweed311

Actually issue is reproducing on macOS, while on Windows I can see Connected status in Storage -> Dashobard -> Data

daweed311 avatar Nov 27 '23 18:11 daweed311

Thank you for reporting this @daweed311, are you still seeing this issue in the Vercel dashboard?

delbaoliveira avatar Nov 28 '23 10:11 delbaoliveira

Yes, only on macOS device

daweed311 avatar Nov 28 '23 18:11 daweed311

I am experiencing the same issue on Windows in addition to the missing_connecting_string error after running the seeding command.

An error occurred while attempting to seed the database: VercelPostgresError: VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.

Bashtiani avatar Nov 28 '23 23:11 Bashtiani

Hi !

Just rename .env.local to .env worked for me

N-Besnard avatar Dec 07 '23 14:12 N-Besnard

$ npm run seed

seed node -r dotenv/config ./scripts/seed.js

node:events:492 throw er; // Unhandled 'error' event ^

Error: getaddrinfo ENOTFOUND ep-cool-butterfly-22386990-pooler.ap-southeast-1.postgres.vercel-storage.com at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:108:26) Emitted 'error' event on WebSocket instance at: at emitErrorAndClose (D:\projects\nextjs-dashboard\node_modules\ws\lib\websocket.js:1016:13) at ClientRequest. (D:\projects\nextjs-dashboard\node_modules\ws\lib\websocket.js:864:5) at ClientRequest.emit (node:events:514:28) at TLSSocket.socketErrorListener (node:_http_client:501:9) at TLSSocket.emit (node:events:514:28) at emitErrorNT (node:internal/streams/destroy:151:8) at emitErrorCloseNT (node:internal/streams/destroy:116:3) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'ep-cool-butterfly-22386990-pooler.ap-southeast-1.postgres.vercel-storage.com' }

Node.js v18.17.0

Kuldeep-truefan avatar Dec 11 '23 10:12 Kuldeep-truefan

I did. But not working for me

MezgebuMeseret1 avatar Jan 10 '24 07:01 MezgebuMeseret1

Having similiar issue (please note this issue is not the connection string issue, slightly different). This symptom has been sited 3 times now (I am on windows, fyi)

  • https://github.com/vercel/next-learn/issues/476
  • https://github.com/vercel/next-learn/issues/484#issuecomment-1849731025

Solutions that will not work for me:

  • adding POSTGRES_URL (it is specified as can be seen in stacktrace below)
  • Renaming .env.development.local OR a .env.local file suggestion. These do NOT exist, I only had .env.example
npm run seed

> seed
> node -r dotenv/config ./scripts/seed.js

node:events:496
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND ep-icy-cell-a47ky353-pooler.us-east-1.postgres.vercel-storage.com
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (C:\work\projects\nextjs-dashboard\node_modules\ws\lib\websocket.js:1016:13)
    at ClientRequest.<anonymous> (C:\work\projects\nextjs-dashboard\node_modules\ws\lib\websocket.js:864:5)
    at ClientRequest.emit (node:events:518:28)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:518:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'ep-icy-cell-a47ky353-pooler.us-east-1.postgres.vercel-storage.com'
}

Node.js v20.11.0

Ideas?

attached a version of data.ts that is an avoidance of the database (and the database issue). Might be useful until someone solves what this problem is. data.txt

warrengoldman avatar Jan 31 '24 21:01 warrengoldman

I'm facing the same issue on a Mac M1 with postgresql installed using brew and POSTGRES_URL properly set (ie I can connect using psql with the same url)

node: v18.19.0

thda avatar Jan 31 '24 22:01 thda

Hi !

Just rename .env.local to .env worked for me

Thanks , it worked for me too I was stuck for atleast 1 hour coz of this problem 🙃

Niskarsh12 avatar Feb 01 '24 13:02 Niskarsh12

Hi ! Just rename .env.local to .env worked for me

Thanks , it worked for me too I was stuck for atleast 1 hour coz of this problem 🙃

I have no .env.local, I had .env.example, which I renamed to .env (as per the tutorial). This did NOT fix it.

warrengoldman avatar Feb 02 '24 15:02 warrengoldman

Hi ! Just rename .env.local to .env worked for me

Thanks , it worked for me too I was stuck for atleast 1 hour coz of this problem 🙃

I have no .env.local, I had .env.example, which I renamed to .env (as per the tutorial). This did NOT fix it.

share the screeshot if you can

Niskarsh12 avatar Feb 02 '24 15:02 Niskarsh12

Hi ! Just rename .env.local to .env worked for me

Thanks , it worked for me too I was stuck for atleast 1 hour coz of this problem 🙃

I have no .env.local, I had .env.example, which I renamed to .env (as per the tutorial). This did NOT fix it.

share the screeshot if you can

See https://github.com/vercel/next-learn/issues/484#issuecomment-1920045791 for details

warrengoldman avatar Feb 02 '24 15:02 warrengoldman

@daweed311 did you find any solution for this?

HadeedShahid avatar Mar 02 '24 18:03 HadeedShahid

no just used static file (to avoid db usage) i attached to issue.

warrengoldman avatar Mar 03 '24 00:03 warrengoldman

@daweed311 did you find any solution for this?

Unfortunately, no. I completed this tutorial on a Windows device.

daweed311 avatar Mar 04 '24 08:03 daweed311

The sql is all wrong.

const users = [ { id: '410544b2-4001-4271-9855-fec4b6a6442a', name: 'User', email: '[email protected]', password: '123456', }, ];

Attempting to insert this data leads to An error occurred while attempting to seed the database: error: trailing junk after numeric literal at or near "410544b"

So basically, the placeholder-js data needs to be sanitized and not evalulate to something else. Postgres is thinking that subtraction is being done due to absence of correct quotes.

openSourceBugs avatar Mar 10 '24 00:03 openSourceBugs

Yes, @openSourceBugs comment is correct.

You need to take 2 steps to fix it:

  1. Use double quotations in the placeholder-daja.js JSON values (as shown below), because the Template Literals (backticks) strings on the seed.js queries are being rendered without single quotes in the values, so they fail. I've attached the fixed JS file, and a snippet below if you wanna do it by hand.
const users = [
  {
    id: "'410544b2-4001-4271-9855-fec4b6a6442a'",
    name: "'User'",
    email: "'[email protected]'",
    password: "'123456'",
  },
];
  1. hashedPassword is a calculated value (doesn't come from the placeholders) so it needs to be fixed in situ: \'${hashedPassword}\' (by adding single quotation marks on seed.js)

Optional:

If you are trying to run the tutorial without connecting to Vercel's cloud, and the @vercel/postgres database client is giving you problems, you can replace it completely with node-postgres.

Just install it with npm install node-postgres then import it in replacement of Vercel's:

//const { db } = require('@vercel/postgres');
const { Client } = require('pg');
const client = new Client({
    user: 'postgres',
    password: 'PASSWORD',
    host: 'localhost',
    port: '5432',
    database: 'nextjs-dashboard-postgres',
});

If you don't set the Config dictionary, it will take the environment variables, just like Vercel's.

And replace all the client.sql with client.query(...) in seed.js like this:

    await client.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
    // Create the "users" table if it doesn't exist
    const createTable = await client.query(`
      CREATE TABLE IF NOT EXISTS users (
        id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        email TEXT NOT NULL UNIQUE,
        password TEXT NOT NULL
      );
    `);

And finally, fix the main function:

async function main() {
  await client.connect();

  await seedUsers(client);
  await seedCustomers(client);
  await seedInvoices(client);
  await seedRevenue(client);

  await client.end();
}

These changes made the trick for me.

Cheers!

placeholder-data.js.txt

The sql is all wrong.

const users = [ { id: '410544b2-4001-4271-9855-fec4b6a6442a', name: 'User', email: '[email protected]', password: '123456', }, ];

Attempting to insert this data leads to An error occurred while attempting to seed the database: error: trailing junk after numeric literal at or near "410544b"

So basically, the placeholder-js data needs to be sanitized and not evalulate to something else. Postgres is thinking that subtraction is being done due to absence of correct quotes.

alfredozorrilla avatar Mar 15 '24 12:03 alfredozorrilla

Yes, @openSourceBugs comment is correct.

You need to take 2 steps to fix it:

  1. Use double quotations in the placeholder-daja.js JSON values (as shown below), because the Template Literals (backticks) strings on the seed.js queries are being rendered without single quotes in the values, so they fail. I've attached the fixed JS file, and a snippet below if you wanna do it by hand.
const users = [
  {
    id: "'410544b2-4001-4271-9855-fec4b6a6442a'",
    name: "'User'",
    email: "'[email protected]'",
    password: "'123456'",
  },
];
  1. hashedPassword is a calculated value (doesn't come from the placeholders) so it needs to be fixed in situ: \'${hashedPassword}\' (by adding single quotation marks on seed.js)

Optional:

If you are trying to run the tutorial without connecting to Vercel's cloud, and the @vercel/postgres database client is giving you problems, you can replace it completely with node-postgres.

Just install it with npm install node-postgres then import it in replacement of Vercel's:

//const { db } = require('@vercel/postgres');
const { Client } = require('pg');
const client = new Client({
    user: 'postgres',
    password: 'PASSWORD',
    host: 'localhost',
    port: '5432',
    database: 'nextjs-dashboard-postgres',
});

If you don't set the Config dictionary, it will take the environment variables, just like Vercel's.

And replace all the client.sql with client.query(...) in seed.js like this:

    await client.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
    // Create the "users" table if it doesn't exist
    const createTable = await client.query(`
      CREATE TABLE IF NOT EXISTS users (
        id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        email TEXT NOT NULL UNIQUE,
        password TEXT NOT NULL
      );
    `);

And finally, fix the main function:

async function main() {
  await client.connect();

  await seedUsers(client);
  await seedCustomers(client);
  await seedInvoices(client);
  await seedRevenue(client);

  await client.end();
}

These changes made the trick for me.

Cheers!

placeholder-data.js.txt

The sql is all wrong. const users = [ { id: '410544b2-4001-4271-9855-fec4b6a6442a', name: 'User', email: '[email protected]', password: '123456', }, ]; Attempting to insert this data leads to An error occurred while attempting to seed the database: error: trailing junk after numeric literal at or near "410544b" So basically, the placeholder-js data needs to be sanitized and not evalulate to something else. Postgres is thinking that subtraction is being done due to absence of correct quotes.

Jesus, thank you man, underrated comment

GodNoden avatar Mar 30 '24 08:03 GodNoden

Comment ajoutées des données à la base de données vercel/postgres lors d'une sumission de formulaire

Offado avatar Apr 12 '24 21:04 Offado

@daweed311 hey there! I have solved this by changing the node version according to package.json. image

shuvo-me avatar Apr 26 '24 06:04 shuvo-me

file name Edit .env.example -> .env

PJW980921 avatar May 01 '24 09:05 PJW980921

still this isn't working on MacOs

dakshika avatar May 26 '24 16:05 dakshika

Hi !

Just rename .env.local to .env worked for me

It's worked for me! Thanks.

sanwinmaung avatar Jun 19 '24 21:06 sanwinmaung

@daweed311 just you need to update the node version

It worked for me

abdulrehman3725 avatar Jun 23 '24 19:06 abdulrehman3725

I am not able to reproduce this. Please open a new issue if you are still having trouble here. Thank you!

We are also making the seeding easier in this PR by allowing you to hit a URL in the browser: https://github.com/vercel/next-learn/pull/764

leerob avatar Jun 23 '24 22:06 leerob