Code Monkey home page Code Monkey logo

Comments (8)

matthuhiggins avatar matthuhiggins commented on June 12, 2024

Hmm.. it's difficult for me to debug this so here are some questions:

Do these same foreign keys show up when doing a "rake db:structure:dump"?
Pick one of these problematic tables and run the big query inside https://github.com/matthuhiggins/foreigner/blob/master/lib/foreigner/connection_adapters/postgresql_adapter.rb

It's going to be one of two problems: Your database actually has duplicates, or the sql query to read the foreign keys is wrong.

from foreigner.

sobrinho avatar sobrinho commented on June 12, 2024

@matthuhiggins, the dump is fine:

---
-- Name: reservation_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY inventory_request_reservation_items
    ADD CONSTRAINT reservation_id_fk FOREIGN KEY (reservation_id) REFERENCES inventory_request_reservations(id);

But query seems wrong:

SELECT tc.constraint_name as name
,ccu.table_name as to_table
,ccu.column_name as primary_key
,kcu.column_name as column
,rc.delete_rule as dependency
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
USING (constraint_catalog, constraint_schema, constraint_name)
JOIN information_schema.referential_constraints rc
USING (constraint_catalog, constraint_schema, constraint_name)
JOIN information_schema.constraint_column_usage ccu
USING (constraint_catalog, constraint_schema, constraint_name)
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.constraint_catalog = '...'
AND tc.table_name = 'inventory_request_reservation_list_items'
AND tc.table_schema = ANY (current_schemas(false));
          name          |             to_table                | primary_key |       column        | dependency 
------------------------+-------------------------------------+-------------+---------------------+------------
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_id_fk      | inventory_request_reservations      | id          | reservation_id      | NO ACTION
 reservation_item_id_fk | inventory_request_reservation_items | id          | reservation_item_id | NO ACTION
 list_id_fk             | inventory_request_reservation_lists | id          | list_id             | NO ACTION
(10 rows)

I don't know how we can fix that. I'm looking the database but dump is generated correctly.

Looks like a issue with query, not my database.

from foreigner.

sobrinho avatar sobrinho commented on June 12, 2024

The query is wrong. I'm working in a fix :)

from foreigner.

matthuhiggins avatar matthuhiggins commented on June 12, 2024

That would be sweet. I am using postgres very lightly and have not seen this problem.

from foreigner.

sobrinho avatar sobrinho commented on June 12, 2024

#47

The pull request fix an issue with repeated names for different constraints :)

from foreigner.

matthuhiggins avatar matthuhiggins commented on June 12, 2024

I like this query a lot more than the existing one!

Is pg_constraint defined per database? For example, if there are two projects running on the same postgres server, will this query retrieve the foreign keys from both databases, or just the one for the connection.

Also, do they need to be left joins?

from foreigner.

sobrinho avatar sobrinho commented on June 12, 2024

Yes, each database has your pg_catalog.pg_constraints table.

And no, the left join could be changed to join, makes more sense.

from foreigner.

sobrinho avatar sobrinho commented on June 12, 2024

Closing. Pull request was accepted :)

from foreigner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.