Travis CI搭配Node.js与PostgreSQL报错:关系'members'不存在
Troubleshooting "relation 'members' does not exist" in Test Database
Hey there! Let's work through this frustrating error you're seeing during your test runs. You mentioned you have a .sql file to create the members table, but the database still claims it doesn't exist—here are the most likely issues and how to fix them:
1. Confirm your test setup runs the SQL migration file
- First, rule out that the .sql file itself works by running it manually against your test database. For PostgreSQL, you'd use a command like:
If the table appears in the database after this, the problem is that your test suite isn't executing this migration step before tests start.psql -d your_test_database_name -f path/to/your/create_members.sql - Check your test runner configuration: make sure there's a setup hook or pre-test script that runs the SQL file. For example, add the migration command as a pre-test step in your CI config (like the Travis job you're using) or your project's test script.
2. Verify you're connected to the correct test database
- It's shockingly easy to accidentally point your test environment to the wrong database. Double-check your test database credentials:
- Confirm the database name, host, username, and port in your test config match the one you migrated.
- Add a quick debug query in your test setup to confirm the current database:
SELECT current_database();
3. Check for case sensitivity mismatches
- PostgreSQL treats table names as case-insensitive only if they're created without quotes. If your .sql file uses something like
"Members"(with quotes) to create the table, queries looking formembers(lowercase, no quotes) won't find it. - Fix this by either:
- Removing quotes from your CREATE TABLE statement so the table is created as
members(lowercase), or - Using the exact quoted name in your test queries.
- Removing quotes from your CREATE TABLE statement so the table is created as
4. Ensure migrations run before test transactions
- Many test frameworks wrap each test in a transaction that rolls back after the test finishes. If your migration runs inside this transaction, the
memberstable will be deleted right after the first test. - Adjust your test setup to run the migration once, before all tests start, outside of any per-test transactions. For example, in Jest you'd use
beforeAll()instead ofbeforeEach()for migration steps.
5. Check migration execution order
- If you have multiple SQL migration files, make sure the one creating
membersruns before any tests or migrations that depend on it. Naming files with a numeric prefix (like001_create_members.sql,002_create_other_table.sql) ensures they run in the correct sequence.
Go through these steps one by one, and you should be able to get the members table recognized by your tests. Good luck!
内容的提问来源于stack exchange,提问作者amishpanda
相关产品推荐
相关产品推荐

