Skip to content

Commit dbde565

Browse files
adela-bytebaseCopilottianzhou
authored
docs: add PG 42703 | PG 28P01 | MySQL 1054 (#1052)
* add PG 42703 | PG 28P01 | MySQL 1054 * Update content/reference/mysql/error/1054-unknown-column-in-field-list.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update content/reference/postgres/error/42703-undefined-column-postgres.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update content/reference/postgres/error/28p01-invalid-password-postgres.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update content/reference/mysql/error/1054-unknown-column-in-field-list.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Tianzhou <t@bytebase.com>
1 parent 2cd90d5 commit dbde565

File tree

5 files changed

+386
-0
lines changed

5 files changed

+386
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "How to Fix MySQL Error 1054: Unknown Column in 'field list'"
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
ERROR 1054 (42S22): Unknown column 'emial' in 'field list'
9+
```
10+
11+
The error can also appear with different contexts:
12+
13+
```sql
14+
ERROR 1054 (42S22): Unknown column 'status' in 'where clause'
15+
ERROR 1054 (42S22): Unknown column 'u.name' in 'on clause'
16+
ERROR 1054 (42S22): Unknown column 'total' in 'order clause'
17+
```
18+
19+
## Description
20+
21+
MySQL raises error 1054 when a query references a column that does not exist in the specified table or expression. The SQLSTATE code is 42S22. This is the MySQL counterpart to [PostgreSQL error 42703](/reference/postgres/error/42703-undefined-column-postgres). The error message helpfully tells you which clause contains the bad reference — `field list` (SELECT), `where clause`, `on clause`, or `order clause`.
22+
23+
## Common Causes
24+
25+
1. **Typo in the column name**: `SELECT emial FROM users` when the column is `email`
26+
2. **Missing or wrong table alias**: Referencing a column without qualifying which table it belongs to in a JOIN
27+
3. **Column alias used in WHERE or HAVING**: MySQL doesn't allow referencing a SELECT alias in the WHERE clause; aliases are only usable in ORDER BY (and sometimes HAVING)
28+
4. **Column dropped or renamed**: A migration changed the column but application queries still use the old name
29+
5. **Wrong table referenced**: The column exists in a different table than expected
30+
6. **Backtick quoting issues**: Reserved words used as column names without backticks
31+
7. **Subquery column not visible**: Referencing a column from an inner query that isn't exposed to the outer query
32+
33+
## How to Fix
34+
35+
### Solution 1: Check the Actual Column Names
36+
37+
```sql
38+
-- List all columns in a table
39+
DESCRIBE users;
40+
41+
-- Or use INFORMATION_SCHEMA
42+
SELECT COLUMN_NAME, DATA_TYPE
43+
FROM INFORMATION_SCHEMA.COLUMNS
44+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users'
45+
ORDER BY ORDINAL_POSITION;
46+
```
47+
48+
### Solution 2: Qualify Columns with Table Aliases in JOINs
49+
50+
```sql
51+
-- Bad: ambiguous column reference
52+
SELECT name FROM orders JOIN customers ON orders.customer_id = customers.id;
53+
54+
-- Good: use table alias
55+
SELECT c.name FROM orders o JOIN customers c ON o.customer_id = c.id;
56+
```
57+
58+
### Solution 3: Don't Use Aliases in WHERE — Repeat the Expression
59+
60+
```sql
61+
-- Bad: alias not visible in WHERE
62+
SELECT CONCAT(first_name, ' ', last_name) AS full_name
63+
FROM users WHERE full_name LIKE '%Smith%';
64+
65+
-- Good: repeat the expression
66+
SELECT CONCAT(first_name, ' ', last_name) AS full_name
67+
FROM users WHERE CONCAT(first_name, ' ', last_name) LIKE '%Smith%';
68+
69+
-- Alternative: use a subquery
70+
SELECT * FROM (
71+
SELECT *, CONCAT(first_name, ' ', last_name) AS full_name FROM users
72+
) t WHERE full_name LIKE '%Smith%';
73+
```
74+
75+
### Solution 4: Quote Reserved Words with Backticks
76+
77+
```sql
78+
-- Bad: 'order' is a reserved word
79+
SELECT order FROM purchases;
80+
81+
-- Good: backtick the reserved word
82+
SELECT `order` FROM purchases;
83+
```
84+
85+
### Solution 5: Fix ON Clause References
86+
87+
When the error points to `'on clause'`, the column reference in a JOIN condition is wrong:
88+
89+
```sql
90+
-- Bad: wrong column name in ON clause
91+
SELECT * FROM orders o JOIN customers c ON o.cust_id = c.id;
92+
93+
-- Good: use the correct column name
94+
SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id;
95+
```
96+
97+
### Solution 6: Verify After Migrations
98+
99+
If a column was recently renamed or dropped:
100+
101+
```sql
102+
-- Check if the old column still exists
103+
SHOW COLUMNS FROM users LIKE 'user_name';
104+
105+
-- Check what columns currently exist
106+
SHOW COLUMNS FROM users;
107+
```
108+
109+
## Common scenarios
110+
111+
**With `SELECT *` in views:** If a view was created with `SELECT *` and the underlying table later dropped a column, queries against the view will fail with 1054. Recreate the view after schema changes.
112+
113+
**In INSERT statements:** `INSERT INTO users (emial) VALUES ('a@b.com')` fails if the column is `email`. The error message will say `Unknown column 'emial' in 'field list'`.
114+
115+
**In ORDER BY with UNION:** When using UNION, ORDER BY can only reference columns from the first SELECT or use positional numbers:
116+
117+
```sql
118+
-- Bad
119+
SELECT id, email FROM users UNION SELECT id, address FROM contacts ORDER BY email;
120+
121+
-- Good: use column position
122+
SELECT id, email FROM users UNION SELECT id, address FROM contacts ORDER BY 2;
123+
```
124+
125+
<HintBlock type="info">
126+
127+
Bytebase's [SQL Review](https://www.bytebase.com/docs/sql-review/review-rules/) can catch unknown column references during change review before they reach production. See also [ERROR 1146: Table Doesn't Exist](/reference/mysql/error/1146-table-doesnt-exist) for the related table-not-found error.
128+
129+
</HintBlock>

content/reference/mysql/error/_layout.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
## Query Syntax & Logic
3333

34+
### [ERROR 1054 (42S22): Unknown column in 'field list'](/reference/mysql/error/1054-unknown-column-in-field-list)
35+
3436
### [ERROR 1064 (42000): You have an error in your SQL syntax](/reference/mysql/error/1064-syntax-error)
3537

3638
### [ERROR 1055 (42000): SELECT list is not in GROUP BY clause and contains nonaggregated column](/reference/mysql/error/1055-select-list-not-in-group-by)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: 'ERROR 28P01: Password Authentication Failed in Postgres'
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
FATAL: password authentication failed for user "app_user"
9+
SQLSTATE: 28P01
10+
```
11+
12+
You may also see a related log entry on the server:
13+
14+
```
15+
DETAIL: User "app_user" has no password assigned.
16+
```
17+
18+
Or when connecting via `psql` or a driver:
19+
20+
```
21+
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed:
22+
FATAL: password authentication failed for user "app_user"
23+
```
24+
25+
## Description
26+
27+
PostgreSQL raises error 28P01 when a client provides a password that does not match the stored credential for the specified user, or when password authentication is required but no valid password is set. The full SQLSTATE code is 28P01 (`invalid_password`). This is the most common connection error for PostgreSQL and is almost always a configuration issue — not a bug in your application.
28+
29+
## Causes
30+
31+
- **Wrong password.** The most obvious cause — the password supplied doesn't match what's stored for that role.
32+
- **Role has no password set.** The role was created with `CREATE ROLE app_user LOGIN` but without a `PASSWORD` clause, so any password attempt fails.
33+
- **Wrong user.** Connecting as `postgres` when the application should connect as `app_user`, or vice versa.
34+
- **pg_hba.conf requires password auth but the role uses a different method.** The `pg_hba.conf` entry says `md5` or `scram-sha-256` but the role's password was set with a different encryption method.
35+
- **Password contains special characters.** Characters like `@`, `#`, `%` in the password may be misinterpreted by connection strings if not properly escaped or quoted.
36+
- **Environment variable or config file has stale credentials.** The password was rotated but `.pgpass`, `PGPASSWORD`, or the application config still has the old one.
37+
- **Connecting to the wrong server.** The credentials are valid on staging but you're connecting to production (or vice versa).
38+
- **SCRAM vs MD5 mismatch.** PostgreSQL 14+ defaults to `scram-sha-256`. If the password was stored as MD5 and `pg_hba.conf` requires SCRAM, authentication fails.
39+
40+
## Solutions
41+
42+
1. **Reset the password:**
43+
44+
```sql
45+
-- Connect as a superuser
46+
ALTER ROLE app_user WITH PASSWORD 'new_secure_password';
47+
```
48+
49+
2. **Verify the role exists and has LOGIN privilege:**
50+
51+
```sql
52+
SELECT rolname, rolcanlogin
53+
FROM pg_roles
54+
WHERE rolname = 'app_user';
55+
```
56+
57+
If `rolcanlogin` is `f`, grant login:
58+
59+
```sql
60+
ALTER ROLE app_user WITH LOGIN;
61+
```
62+
63+
3. **Check pg_hba.conf authentication method:**
64+
65+
```bash
66+
# Find the active pg_hba.conf
67+
psql -U postgres -c "SHOW hba_file;"
68+
```
69+
70+
Look for the line matching your connection. Common entries:
71+
72+
```
73+
# TYPE DATABASE USER ADDRESS METHOD
74+
host all all 127.0.0.1/32 scram-sha-256
75+
host all all 0.0.0.0/0 md5
76+
```
77+
78+
After editing, reload:
79+
80+
```sql
81+
SELECT pg_reload_conf();
82+
```
83+
84+
4. **Fix SCRAM vs MD5 mismatch:**
85+
86+
```sql
87+
-- Check current password encryption setting
88+
SHOW password_encryption;
89+
90+
-- If it shows 'scram-sha-256' but pg_hba.conf says 'md5', either:
91+
-- Option A: Change pg_hba.conf to scram-sha-256 (recommended)
92+
-- Option B: Set encryption to md5 and reset the password
93+
SET password_encryption = 'md5';
94+
ALTER ROLE app_user WITH PASSWORD 'the_password';
95+
```
96+
97+
5. **Check .pgpass or connection string:**
98+
99+
```bash
100+
# .pgpass format: hostname:port:database:username:password
101+
cat ~/.pgpass
102+
103+
# Ensure correct permissions
104+
chmod 600 ~/.pgpass
105+
```
106+
107+
For connection strings, escape special characters:
108+
109+
```
110+
postgresql://app_user:p%40ssw0rd@localhost:5432/mydb
111+
```
112+
113+
`@``%40`, `#``%23`, `%``%25`
114+
115+
6. **Verify you're connecting to the right server:**
116+
117+
```sql
118+
-- After connecting, check where you are
119+
SELECT inet_server_addr(), inet_server_port(), current_user, current_database();
120+
```
121+
122+
7. **Check server logs for more detail:**
123+
124+
```bash
125+
# The server log often has a DETAIL line explaining what went wrong
126+
tail -20 /var/log/postgresql/postgresql-16-main.log
127+
```
128+
129+
## Common scenarios
130+
131+
**In Docker and containers:** The `POSTGRES_PASSWORD` environment variable only sets the superuser password on first initialization. If you change it in `docker-compose.yml` and restart without deleting the volume, the old password persists. Either drop the volume (`docker volume rm ...`) or connect and run `ALTER ROLE`.
132+
133+
**After PostgreSQL major version upgrade:** PostgreSQL 14 changed the default `password_encryption` from `md5` to `scram-sha-256`. After upgrading, existing MD5 passwords still work if `pg_hba.conf` uses `md5`, but if you change `pg_hba.conf` to `scram-sha-256`, all users must reset their passwords.
134+
135+
**In connection pools (PgBouncer, Pgpool-II):** The pooler authenticates users independently. If the database password is changed but the pooler's `userlist.txt` or auth config isn't updated, connections through the pool fail with 28P01 even though direct connections work.
136+
137+
**With cloud-managed databases (RDS, Cloud SQL, Azure):** Cloud providers manage `pg_hba.conf` internally. If you get 28P01 on RDS, verify the password via the AWS console or `aws rds modify-db-instance`. For Cloud SQL, use `gcloud sql users set-password`.
138+
139+
<HintBlock type="info">
140+
141+
Bytebase provides a [secure credential management workflow](https://www.bytebase.com/docs/security/secret/) that avoids sharing database passwords directly. For more on PostgreSQL access control, see [Database Access Control Best Practices](/blog/database-access-control-best-practices/).
142+
143+
</HintBlock>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 'ERROR 42703: Column Does Not Exist in Postgres'
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
ERROR: column "emial" does not exist
9+
LINE 1: SELECT emial FROM users;
10+
^
11+
SQLSTATE: 42703
12+
```
13+
14+
## Description
15+
16+
PostgreSQL raises error 42703 when a query references a column that the database cannot find in the target table or expression. The full SQLSTATE code is 42703 (`undefined_column`). This is one of the most common PostgreSQL errors — closely related to [42P01 (undefined table)](/reference/postgres/error/42p01-undefined-table-postgres) — and usually traces back to a spelling mistake, a missing alias, or a schema mismatch.
17+
18+
## Causes
19+
20+
- **Typo in the column name.** `SELECT emial FROM users` fails because the column is actually `email`.
21+
- **Missing table alias or wrong alias.** In a multi-table query, referencing `SELECT name FROM orders JOIN customers c ON ...` fails if `name` exists in `customers` but not `orders` and no alias is specified.
22+
- **Case sensitivity.** If a column was created with double quotes (`"Status"`), you must always reference it as `"Status"`. Without quotes, PostgreSQL folds identifiers to lowercase, so `SELECT Status` becomes `SELECT status` which won't match `"Status"`.
23+
- **Column dropped or renamed.** A migration renamed `user_name` to `username` but application code still references the old name.
24+
- **Wrong table in a JOIN.** The column exists in a different table than the one being referenced.
25+
- **Subquery or CTE column not exposed.** A subquery selects `id` but the outer query references `user_id` from that subquery.
26+
- **Schema mismatch after migration.** The application expects a column that a migration hasn't created yet, or that was rolled back.
27+
28+
## Solutions
29+
30+
1. **Check the actual column names in the table:**
31+
32+
```sql
33+
SELECT column_name, data_type
34+
FROM information_schema.columns
35+
WHERE table_name = 'users'
36+
ORDER BY ordinal_position;
37+
```
38+
39+
2. **Use a table alias to disambiguate columns in JOINs:**
40+
41+
```sql
42+
-- Bad: ambiguous or wrong table
43+
SELECT name FROM orders JOIN customers ON orders.customer_id = customers.id;
44+
45+
-- Good: qualify with alias
46+
SELECT c.name FROM orders o JOIN customers c ON o.customer_id = c.id;
47+
```
48+
49+
3. **Handle case-sensitive column names:**
50+
51+
```sql
52+
-- If created with double quotes
53+
SELECT "Status" FROM orders;
54+
55+
-- Check actual casing
56+
SELECT column_name FROM information_schema.columns
57+
WHERE table_name = 'orders' AND column_name ILIKE '%status%';
58+
```
59+
60+
4. **Verify the column in psql:**
61+
62+
```sql
63+
\d users
64+
```
65+
66+
5. **Check subquery or CTE column names:**
67+
68+
```sql
69+
-- Bad: outer query references a column the CTE doesn't expose
70+
WITH active AS (SELECT id FROM users WHERE active = true)
71+
SELECT user_id FROM active;
72+
73+
-- Good: match the column name
74+
WITH active AS (SELECT id FROM users WHERE active = true)
75+
SELECT id FROM active;
76+
```
77+
78+
6. **Run pending migrations:**
79+
80+
If the column should have been added by a migration, verify it was applied:
81+
82+
```bash
83+
# Check migration status
84+
flyway info
85+
# Or check the change history in Bytebase
86+
```
87+
88+
## Common scenarios
89+
90+
**In ORMs and application code:** ORMs map model fields to column names. If you rename a field in your model but forget to generate a migration, or if the migration hasn't been applied, PostgreSQL will report 42703 at runtime. Check `\d tablename` against your model definition.
91+
92+
**In GROUP BY and ORDER BY:** PostgreSQL doesn't allow referencing column aliases in `WHERE` or `HAVING` (though `ORDER BY` is fine). `SELECT email AS e FROM users WHERE e LIKE '%@%'` fails with 42703 because aliases aren't visible in `WHERE`.
93+
94+
```sql
95+
-- Bad
96+
SELECT email AS e FROM users WHERE e LIKE '%@%';
97+
98+
-- Good
99+
SELECT email AS e FROM users WHERE email LIKE '%@%';
100+
```
101+
102+
**After ALTER TABLE:** If a column was renamed with `ALTER TABLE users RENAME COLUMN user_name TO username`, any view, function, or application query still using the old name will fail with 42703. Search your codebase for the old column name.
103+
104+
<HintBlock type="info">
105+
106+
Bytebase's [SQL Review](https://www.bytebase.com/docs/sql-review/review-rules/) can catch references to non-existent columns during change review, before they reach production. See also [ERROR 42P01: Relation Does Not Exist](/reference/postgres/error/42p01-undefined-table-postgres) for the related table-not-found error.
107+
108+
</HintBlock>

0 commit comments

Comments
 (0)