initial commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# Adding Fields to PocketBase Collection Schema
|
||||
|
||||
PocketBase silently drops fields not defined in the collection schema — HTTP 200, no error, data just disappears. When you add new fields to the data model, you MUST also add them to the PocketBase schema via API.
|
||||
|
||||
## The problem
|
||||
|
||||
Services saved with `recommendation` and `explanation` fields appeared to save successfully, but the fields were missing when read back. Root cause: the `services` collection schema only had `name`, `price`, `description`, `category`, `duration` — unknown fields were silently stripped on create/update.
|
||||
|
||||
## Detection
|
||||
|
||||
Query the collection schema to see current fields:
|
||||
|
||||
```python
|
||||
# Auth + list collections
|
||||
auth = api("POST", "/api/collections/_superusers/auth-with-password", {
|
||||
"identity": "admin@example.com",
|
||||
"password": "password"
|
||||
})
|
||||
token = auth["token"]
|
||||
cols = api("GET", "/api/collections", token=token)
|
||||
svc = next(c for c in cols["items"] if c["name"] == "services")
|
||||
print([f["name"] for f in svc["fields"]])
|
||||
# ['id', 'userId', 'name', 'description', 'price', 'category', ...]
|
||||
```
|
||||
|
||||
## Fix: PATCH the collection
|
||||
|
||||
Add missing fields by PATCHing the complete fields array (including existing fields):
|
||||
|
||||
```python
|
||||
existing_fields = svc["fields"]
|
||||
new_fields = [
|
||||
{"autogeneratePattern":"","hidden":False,"id":"text_rec_new","max":0,"min":0,
|
||||
"name":"recommendation","pattern":"","presentable":False,"primaryKey":False,
|
||||
"required":False,"system":False,"type":"text"},
|
||||
{"autogeneratePattern":"","hidden":False,"id":"text_exp_new","max":0,"min":0,
|
||||
"name":"explanation","pattern":"","presentable":False,"primaryKey":False,
|
||||
"required":False,"system":False,"type":"text"},
|
||||
]
|
||||
result = api("PATCH", f"/api/collections/{svc['id']}",
|
||||
{"fields": existing_fields + new_fields}, token=token)
|
||||
```
|
||||
|
||||
**Important**: You must send the COMPLETE fields array, not just the new fields. PocketBase replaces the entire schema. Each field needs a unique `id` string — use descriptive names like `text_rec_001` or UUIDs.
|
||||
|
||||
## Full script
|
||||
|
||||
See `scripts/pb_add_fields.py` for a reusable script that auths, lists collections, and adds specified fields.
|
||||
Reference in New Issue
Block a user