Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: FOREIGN KEY constraint failed #1298

Open
dreamid27 opened this issue May 24, 2024 · 4 comments
Open

Error: FOREIGN KEY constraint failed #1298

dreamid27 opened this issue May 24, 2024 · 4 comments
Assignees

Comments

@dreamid27
Copy link

dreamid27 commented May 24, 2024

Hi Electric Developer, I encountered an error when the product table and the product_images table have a relationship.

OS : Mac
Browser: Chrome
Electric Version: 0.10.0

Error: FOREIGN KEY constraint failed

Screen Shot 2024-05-24 at 09 38 08

the table structure

Screen Shot 2024-05-24 at 09 39 17

I'm trying to query in my localhost, it's not problem and fetch as expected. and also the data is not filled on supabase.

SELECT p.id,
        p.name,
        p.price,
        pi.id as product_image_id,
        pi.product_image as product_image
    FROM 
        product p
    LEFT JOIN 
        product_images pi ON p.id = pi.product_id;

Screen Shot 2024-05-24 at 09 43 26

Screen Shot 2024-05-24 at 09 42 55

if you need the migration syntax

CREATE TABLE IF NOT EXISTS product_images (
    id UUID PRIMARY KEY,
    product_id UUID NOT NULL,
    product_image TEXT,
    FOREIGN KEY (product_id) REFERENCES product(id)
);

ALTER TABLE product_images ENABLE ELECTRIC;
Copy link

linear bot commented May 24, 2024

@alco
Copy link
Member

alco commented May 24, 2024

Hey @dreamid27. Could you share your sync code where you define the shape to be synced from the Electric server?

@dreamid27
Copy link
Author

also the data is not filled

sure, this

Screen Shot 2024-05-24 at 16 34 13

@msfstef
Copy link
Contributor

msfstef commented May 30, 2024

@dreamid27 from your sync code it seems like you are only syncing the product table and not the product_image table, and are executing a join on a table that's not been synced.

Try to change the sync line to:

const shape = await db.product.sync({
  include: {
    product_image: true
  }
})

this will sync the product table as well as any entries in the product_image table that have an FK relationship to any of the synced rows in the product table. Now your query should be able to execute since all the relevant data is present on the device.

Let us know if this resolves the issue!

@msfstef msfstef self-assigned this May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants