Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Enforcing Unique IDs #81
Comments
|
Currently a creating a record with an id that equals another existing record would just overwrite it - that's not expected functionality. I agree that this should be added. Ideally this will be added to the database layer For now, you can patch this easily at the ORM layer before a full feature is added (might differ slightly for TS): class MyBaseModel extends Model {
static create(props) {
if (props.hasOwnProperty(this.idAttribute) && this.hasId(props[this.idAttribute])) {
throw new Error(`Id with ${props[this.idAttribute]} already exists`);
}
return super.create(props);
}
} |
|
yup, makes sense, just checking on this. That's what I added in my basemodel. |
|
I created the same object that u can extend to make and update if we currently have the record on our store or create if not: import { Model } from 'redux-orm';
export default class DopeModel extends Model {
static create(props) {
if (props.hasOwnProperty(this.idAttribute) && this.hasId(props[this.idAttribute])) {
const model = this.withId(props.id);
return model.update(props);
}
return super.create(props);
}
} |
|
can we have idAttribute with 2 columns or two props of the object ? |
|
@udayreddy This is currently not possible. And I'm not planning this myself because there's already more than enough on the roadmap. But would happily merge a PR if tested, consistent with current memoization and non-breaking. I guess a reasonable API would be The obvious easy workaround would be to store them in the same field instead ( And for anyone passing by, the |
|
Thank you for the kudos @haveyaseen 🤟🏻 |


Is this possible to do? Seems like redux-orm would be confused if there were multiple records with the same ID since it wouldn't know which one to fetch. Do you plan to add a check against this?
In many cases, you will be getting records from an API which is the "source of truth". You then want to add it to the orm, but it's possible to manually specify the ID and keep essentially creating the same one. Seems like it should be the responsibility of the ORM to prevent this behavior since it then cannot operate correctly.