Welcome Guest, Not a member yet? Register   Sign In
Datamapper and ID field
#1

[eluser]gte451f[/eluser]
I think the Datamapper project is great! I'm hung up on one thing.

Why does it require EVERY table to have a auto increment pk of "ID"?

This seems wasteful for a common form of Parent -> Child relationships.

Consider the following One to One....

User
-------------
id
first_name
last_name

Employee (a type of user)
------------------------------
user_id
department


The Employee record descends from one User record and therefore the Employee->user_id value IS unique and can act as the PK.

Yet Datamapper doesn't play well with this arrangement. It forces me to add an "ID" field to the employee table.

Ack!
#2

[eluser]WanWizard[/eluser]
You need the primary key to uniquely identify a record. In your example, there is no way to do that, so how are you going to delete or modify a single employee record?

You may think that you have defined a one-to-one (which by the way isn't a relationship that exists in Datamapper), but you can easily have multiple employee records with the same user_id (for example someone that works part-time for two departments?).

You will have to make this a one-to-one in your application code, by making sure you don't create one with a given user_id while another one exists. Datamapper is not aware of that.

For this to work, you will need a "belongs_to" type of relation, which doesn't exist in Datamapper. It will also need complete new logic for interacting with tables, making the library even more complex.
#3

[eluser]gte451f[/eluser]
[quote author="WanWizard" date="1335508827"]You need the primary key to uniquely identify a record. In your example, there is no way to do that, so how are you going to delete or modify a single employee record?[/quote]

It may not have been clear from the example, but the Employee->user_id is taken from User->id. It is intended as a unique Foreign Key. Therefore, the employee->user_id can and would be unique under such an arrangement.

[quote author="WanWizard" date="1335508827"]
You may think that you have defined a one-to-one (which by the way isn't a relationship that exists in Datamapper),
[/quote]
I'm confused...here is a One-To-One in the user docs.
http://datamapper.wanwizard.eu/pages/relationtypes.html

Perhaps we are talking about different things?

[quote author="WanWizard" date="1335508827"]
... but you can easily have multiple employee records with the same user_id (for example someone that works part-time for two departments?).
[/quote]

Of course it is *possible* to have multiple employee records but misses the point of the example. This example is intended to illustrate a common relationship where a "Child" table bases its existence on a "Parent" table. In the relationship, the child takes a Foreign Key from the parent that is unique to the child table.



[quote author="WanWizard" date="1335508827"]
You will have to make this a one-to-one in your application code, by making sure you don't create one with a given user_id while another one exists. Datamapper is not aware of that.

For this to work, you will need a "belongs_to" type of relation, which doesn't exist in Datamapper. It will also need complete new logic for interacting with tables, making the library even more complex.[/quote]

Understood. I'm not really looking to add a belongs to type relationships because it would be difficult!

Rather, I'd like to drop the requirement that every datamaper model require a table to have a Auto Increment PK called ID. In some examples the child table doesn't need this type of field since it takes its parent table Primary Key.

Specifically, could we optionally drop the requirement that the PK name = "Id" and could be drop the Auto Increment part as well?
#4

[eluser]WanWizard[/eluser]
[quote author="gte451f" date="1335900102"]It may not have been clear from the example, but the Employee->user_id is taken from User->id. It is intended as a unique Foreign Key. Therefore, the employee->user_id can and would be unique under such an arrangement.[/quote]
It was very clear. Still, EVERY Datamapper table needs a primary key, and it HAS to be called 'id'.

[quote author="gte451f" date="1335900102"][quote author="WanWizard" date="1335508827"]
You may think that you have defined a one-to-one (which by the way isn't a relationship that exists in Datamapper),
[/quote]
I'm confused...here is a One-To-One in the user docs.
Perhaps we are talking about different things?[/quote]
What I mean is that Datamapper currently has no logic to enforce a one-to-one, it doesn't delete a relation when you set a second one. So technically a one-to-one is the same as a one-to-many.

[quote author="gte451f" date="1335900102"]
Rather, I'd like to drop the requirement that every datamaper model require a table to have a Auto Increment PK called ID. In some examples the child table doesn't need this type of field since it takes its parent table Primary Key.

Specifically, could we optionally drop the requirement that the PK name = "Id" and could be drop the Auto Increment part as well?[/quote]
That would mean rewriting large parts of Datamapper, since everything is done on this column, and it's hardcoded.

This will be addressed in 2.0, for which no release date exists at this moment (I wait for CI 3.0 to be released, currently there are to many changes going on in the core).




Theme © iAndrew 2016 - Forum software by © MyBB