question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Cannot find primary key" error for postgres tbl

See original GitHub issue

I’m trying to run platformatic on an existing postgres db by setting connection string directly and bypassing migration. on npx platformatic db start I get the following error Error: Cannot find primary key for Advert entity.

The adverts table has the following DDL:

-- idkdb.adverts definition

-- Drop table

-- DROP TABLE idkdb.adverts;

CREATE TABLE idkdb.adverts (
	id serial4 NOT NULL,
	image_url varchar NOT NULL,
	thumbnail_url varchar NOT NULL,
	name varchar NOT NULL,
	description varchar NOT NULL,
	link varchar NOT NULL,
	last_displayed timestamp NULL,
	created_at timestamp NOT NULL DEFAULT now(),
	CONSTRAINT adverts_pkey PRIMARY KEY (id)
);

Any idea what could be causing this?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deppecommented, Oct 11, 2022

Discussed with @ivan-tymoshenko offline. This was caused by using a connection string for a user without DDL permissions. See https://github.com/platformatic/platformatic/issues/161 for follow-up.

While troubleshooting, we ran into another a similar “Cannot find primary key” error. In this case, the table only has a foreign key without a primary key:

-- idkdb.documents_metadata definition

-- Drop table

-- DROP TABLE idkdb.documents_metadata;

CREATE TABLE idkdb.documents_metadata (
    document_id int4 NOT NULL,
    star bool NOT NULL DEFAULT false,
    starers_uuid int4 NULL,
    net_likes int4 NOT NULL DEFAULT 0,
    updated_at timestamp NOT NULL DEFAULT now(),
    CONSTRAINT documents_metadata_document_id_fkey FOREIGN KEY (document_id) REFERENCES idkdb.documents(id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX documents_metadata_document_id_idx ON idkdb.documents_metadata USING btree (document_id);

-- Table Triggers

create trigger documents_metadata_set_updated_at_timestamp before
update
    on
    idkdb.documents_metadata for each row execute function set_updated_at_timestamp();
0reactions
mcollinacommented, Oct 11, 2022

We won’t have endpoints that return entity by id, but we still can have an endpoint that return all entities. WDYT?

Indeed, it would essentially be a read-only table. What’s interesting is that you could still use it for nested references.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres: Unable to set primary key on a column
Firstly you are missing data types for user_id and matched_profile . You are also using serial when you should be using INT GENERATED...
Read more >
Cannot get anything with primaryKey to work with Postgres ...
Imagine a scenario when the table is not created in the database yet. Running this code 2 times in a row should be...
Read more >
Documentation: 8.2: CREATE TABLE - PostgreSQL
The primary key constraint specifies that a column or columns of a table may contain ... Currently, CHECK expressions cannot contain subqueries nor...
Read more >
Troubleshooting migration tasks in AWS Database Migration ...
To solve this issue, the current approach is to first precreate the tables and primary keys in the target database. Then use a...
Read more >
Database schema missing many primary keys - breaks ...
On both modern MySQL and PostgreSQL systems, replication requires tables have primary keys in order to function properly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found