Code Monkey home page Code Monkey logo

Comments (8)

ricardogama avatar ricardogama commented on September 25, 2024 1

I'll try to take a look until the end of the week.

from bookshelf-cascade-delete.

ricardogama avatar ricardogama commented on September 25, 2024 1

It looks that the default foreign key is being calculated correctly, from the Bookshelf docs:

ForeignKey in the Target model. By default, the foreignKey is assumed to be the singular form of this model's tableName, followed by _id

So following #8, the default foreign key would be Post_id but the actual foreign key of the Category table is post_id, and therefore the error is not related to quoting columns.

@petetnt Can you give a concrete example where this is failing? Otherwise I cannot reproduce the error.

from bookshelf-cascade-delete.

petetnt avatar petetnt commented on September 25, 2024

Any pointers on where the bug is? This is blocking my usage of bookshelf-cascade-delete and I might as well take a stab on fixing it for good.

from bookshelf-cascade-delete.

petetnt avatar petetnt commented on September 25, 2024

Actually I think my issue might be unrelated due to misunderstanding what default foreign key meant in this context (I assumed it as "the foreign key the field has in the DB", as I think my issues spawned from having a non-default (in Bookshelf.js sense) foreign key

For example something like

const addressModel = db.Model.extend({
 tableName: "address_table";
 ...
})
const fooModel = db.Model.extend({
  tableName: "foo_table",
  billing_address() {
   return this.belongsTo(addressModel, "billing_address");
 }
}, {
  dependents: ["billing_address"]
})

and the table structure would be something like:

knex.schema.table("address_table", (table) => {
  table.increments().primary();
  // other fields of an address
});

knex.schema.table("foo_table", (table) => {
  table.increments().primary();
  table.integer("billing_address");
  table.foreign("billing_address").references("address_table.id");
});

calling fooModel.forge({id: 1}).destroy() would result into delete from "address_table" where "id" IN ('123456') - column "id" does not exist.

Hopefully that made some sense, I wrote it out of my mind quite hastily. The example is a bit weird but it makes more sense with the actual names of the fields. That said, fooModel.forge({id: 1}).fetch({withRelated: ["billing_address"]}) works as it should:

fooModel.forge({id: 1}).fetch().then(address => {
  console.log(address);
  // results in:
  {
     "id": 1,
     "billing_address": 123456
  }
});

// with related:

fooModel.forge({id: 1}).fetch({withRelated: ["billing_address"]}).then(address => {
  console.log(address);
  // results in:
  {
    "id": 1,
    "billing_address": {
       "id": 123456,
       "other": "fields", 
       "of": "an", 
       "address": "!!!"
    }
  }
});

edit: Modified the example to be bit more concrete.

from bookshelf-cascade-delete.

ricardogama avatar ricardogama commented on September 25, 2024

I believe the issue you're facing is the lack of support for belongsTo relations, which is fixed on #25 but it can't be merged until next Bookshelf release.

Anyway I'll try to create a fix for this by tomorrow and make a release, thanks for the report!

from bookshelf-cascade-delete.

petetnt avatar petetnt commented on September 25, 2024

@ricardogama neat 👍 I'll see and try running the queries against the PR (and Bookshelf master).

Thanks for the replies and keep up the great work!

from bookshelf-cascade-delete.

ricardogama avatar ricardogama commented on September 25, 2024

I believe you have to use the bookshelf/bookshelf#1419 branch for that to work:

"devDependencies": {
    "bookshelf": "https://github.com/seegno-forks/bookshelf.git#bugfix/belongs-to-parent-id-attribute"
}

from bookshelf-cascade-delete.

ricardogama avatar ricardogama commented on September 25, 2024

@petetnt It seems Bookshelf has new maintainers and has been active so I got some time to review some pending issues.

After some thought I realised that dependents of the belongs to relation should not be deleted in the first place, since it's a reverse relation and therefore the model that's being deleted is the one who has the foreign key.

Does this make sense in your case? If so can you confirm that #36 solves this issue?

Cheers!

from bookshelf-cascade-delete.

Related Issues (13)

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.