{"action":"closed","issue":{"url":"https://api.github.com/repos/friemen/aggregate/issues/7","repository_url":"https://api.github.com/repos/friemen/aggregate","labels_url":"https://api.github.com/repos/friemen/aggregate/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/friemen/aggregate/issues/7/comments","events_url":"https://api.github.com/repos/friemen/aggregate/issues/7/events","html_url":"https://github.com/friemen/aggregate/issues/7","id":167659442,"number":7,"title":"Parent record wrongly deleted when creating child records","user":{"login":"amoe","id":883725,"avatar_url":"https://avatars.githubusercontent.com/u/883725?v=3","gravatar_id":"","url":"https://api.github.com/users/amoe","html_url":"https://github.com/amoe","followers_url":"https://api.github.com/users/amoe/followers","following_url":"https://api.github.com/users/amoe/following{/other_user}","gists_url":"https://api.github.com/users/amoe/gists{/gist_id}","starred_url":"https://api.github.com/users/amoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amoe/subscriptions","organizations_url":"https://api.github.com/users/amoe/orgs","repos_url":"https://api.github.com/users/amoe/repos","events_url":"https://api.github.com/users/amoe/events{/privacy}","received_events_url":"https://api.github.com/users/amoe/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-26T17:01:49Z","updated_at":"2016-07-26T20:43:11Z","closed_at":"2016-07-26T20:43:11Z","body":"    (def db-host \"localhost\")\r\n    (def db-port 5432)\r\n    (def db-name \"test\")\r\n    (def db-spec {:classname \"org.postgresql.Driver\"\r\n          :subprotocol \"postgresql\"\r\n          :subname (str \"//\" db-host \":\" db-port \"/\" db-name)})\r\n\r\n    (def model\r\n      (agg/make-er-config\r\n       (agg/entity :book\r\n                   (agg/->mn :authors :author {:query-fn\r\n                                               (agg/make-query-<many>-fn :author\r\n                                                                         :author_books    ; link table\r\n                                                                         :book_id\r\n                                                                         :author_id)\r\n                                               :update-links-fn \r\n                                               (agg/make-update-links-fn :author_books    ; link table\r\n                                                                         :book_id\r\n                                                                         :author_id)}))\r\n       (agg/entity :author\r\n                   (agg/->1 :company :company {:fk-kw :company_id\r\n                                               :owned? true    ;; !!! toggle false to make it work\r\n                                               }))   \r\n       (agg/entity :company)))\r\n\r\n\r\n    (def ddl\r\n      [\"DROP TABLE IF EXISTS book, company, author, author_books\"\r\n       \"CREATE TABLE book (id SERIAL PRIMARY KEY, book_name VARCHAR(80));\"\r\n       \"CREATE TABLE company (id SERIAL PRIMARY KEY, company_name VARCHAR(80))\"\r\n       \"CREATE TABLE author (id SERIAL PRIMARY KEY, author_name VARCHAR(80), company_id INTEGER NOT NULL REFERENCES company (id));\"\r\n       \"CREATE TABLE author_books (author_id INTEGER NOT NULL REFERENCES author (id), book_id INTEGER NOT NULL REFERENCES book (id))\"])\r\n\r\n    (declare attempt-save)\r\n\r\n    (defn do-test []\r\n      (doseq [t ddl]\r\n        (jdbc/execute! db-spec [t]))\r\n\r\n      (let [book-id (-> (agg/save! model db-spec :book {:book_name \"The Great Gatsby\" :authors []}) :id)]\r\n        (let [company-id (-> (agg/save! model db-spec :company {:company_name \"Fishbulb\"}) :id)]\r\n          (attempt-save company-id book-id))))\r\n\r\n\r\n    (defn attempt-save [company-id book-id]\r\n        (agg/save! model db-spec :book\r\n                   {:book_name \"Moby Dick\"\r\n    ;                :authors [{:author_name \"Herman Melville\" :company {:id company-id}}]\r\n                    :authors [{:author_name \"Herman Melville\" :company_id company-id}]\r\n                    :id book-id})\r\n        (println \"Hello, World!\"))\r\n\r\nThe model here is a little complex, but i want to *write to a many-to-many of entity `X` and create a new record of type `Y`, where that child record references an existing record of type `Z` through a `->1` relationship*.\r\n\r\nFor some reason `owned?` has to be set to `false` to make the above code work.  I don't know why though.  The documentation describes this as \"if the whole is deleted the parts vanish\".  Therefore, the part would be the `company` and the whole would be the `author`.  Since the author is never deleted in this example, the company should not be deleted by `attempt-save`, but it seems that it is."}}
{"actor":{"display_login":"friemen"}}