datamapper multi-field unique index - ruby

In Datamapper, how would one specify the the combination of two fields must be unique. For example categories must have unique names within a domain:
class Category
include DataMapper.resource
property :name, String, :index=>true #must be unique for a given domain
belongs_to :domain
end

You have to create a unique index for the two properties:
class Category
include DataMapper::Resource
property :name, String, :unique_index => :u
property :domain_id, Integer, :unique_index => :u
belongs_to :domain
end

Actually, John, Joschi's answer is correct: the use of named :unique_index values does create a multiple-column index; it's important to read the right-hand side of those hash-rockets (i.e., if it had just been true, you would be right).

Did you try to define both properties as keys? Not sure I have tried it but that way they should become a composite key.
property :name, String, :key => true
property :category, Integer, :key => true

Related

Ruby: undefined method `delete', deleting parent and child data at the same time

I'm a newbie programmer, basically I'm trying to delete a record from a database table, however this record would be used on a child table with a composed primary key taking both from different tables.
This is the class of the table I need to delete the child data from.
class Detalle_Mapa
include DataMapper::Resource
storage_names[:default] = 'detalle_ma'
property :clave_mapa, String, :length => 2, :required => true, :key => true
property :clave_asig, String, :length => 10, :required => true, :key => true
property :clave_cuatri, String, :length => 2, :required => true
belongs_to :mapa_curricular, 'Mapa_Curricular', :child_key => [:clave_mapa], :key => true
belongs_to :cuatrimestre, 'Cuatrimestre', :child_key => [:clave_cuatri], :key => true
belongs_to :asignatura, 'Asignatura', :child_key => [:clave_asig]
end
So now when I try to delete data from the parent table, it won't delete it since the PK is being used on the child table for other data. If this was normal SQL sintax it wouldn't be a big deal but I'm having a hard time finding a way around it on Ruby.
This is the basic method that works when deleting data that's not being used as a FK.
delete '/deleteMapCurricular/:clave_mapa' do
#mapa = Mapa_Curricular.get(params[:clave_mapa])
if #mapa
#mapa.destroy
redirect '/catMapCurricular'
end
end
And this is one of the ways I've tried to delete the child data, which is clearly not right...
Detalle_Mapa.where(:clave_mapa => [params[:clave_mapa]]).delete_all
Is there an easy way to just delete a bunch of data from a database that I'm not aware of? Or what I'm I not getting about this ActiveRecords on Ruby?
I'm sorry if the question is ambiguous or if I'm not explaining myself clearly, I usually find everything on forums and there is no need to ask myself. Any help will be greatly appreciated :)
Sorry for not giving out the exact answer. But I can tell a concept in this answer
class User < ActiveRecord::Base
has_many :fruits, dependent: :destroy
end
class Fruit < ActiveRecord::Base
belongs_to :user
end
So in the above example, if you delete a user, the associated fruits for that user will also be deleted automatically.

create unique constraints per user

I am building a small app and at this point I am creating the database schema. I use PostgreSQL with Sequel and I have the two migrations:
Sequel.migration do
change do
Sequel::Model.db.run 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
create_table :user do
String :id, :type => :uuid, :primary_key => true, :default => Sequel.function(:uuid_generate_v4)
DateTime :created_at
DateTime :updated_at
index :id, :unique => true
end
end
end
Sequel.migration do
change do
Sequel::Model.db.run 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
create_table :location do
String :id, :type => :uuid, :primary_key => true, :default => Sequel.function(:uuid_generate_v4)
foreign_key :user_id, :user, :type => :uuid
String :name, :unique => true, :null => false # TODO: unique, per user
Float :latitude, :null => false
Float :longitude, :null => false
DateTime :created_at
DateTime :updated_at
index :id, :unique => true
full_text_index :name, :index_type => :gist
end
end
end
As you can see the name column on the location table is unique, but I am in doubt about it. Because I want to establish a unique constraint on the name, but per user, so a user can have only one location with the same name but in the entire table many users can have locations with the same name (the relation between user and location is a one to many).
I suppose my unique constraint added on the column will make column generally unique, so amongst all users the location name must be unique. If so, how do I create a constraint that makes the name unique on a per user basis?
Just create the unique constraint over both columns:
UNIQUE (user_id, name)
Per documentation:
This specifies that the combination of values in the indicated columns
is unique across the whole table, though any one of the columns need
not be (and ordinarily isn't) unique.
But from the looks of it, you really want another table user_location than implements an n:m relation between locations and users - with a primary key on (user_id, location_id).
And don't call the first table "user", that's a reserved word in standard SQL and in Postgres and shouldn't be used as identifier.

One-to-one DataMapper association

I'm very new to DataMapper, and I'm trying to create models for the following scenario:
I've got a number of users (with a user name, password etc.), who can also be players or referees or both (so Single Table Inheritance is not an option). The base models would be:
class User
include DataMapper::Resource
property :id, Serial
# Other user properties go here
end
class Player
include DataMapper::Resource
property :id, Serial
# Other player properties go here
# Some kind of association goes here
end
class Referee
include DataMapper::Resource
property :id, Serial
# Other referee properties go here
# Some kind of association goes here
end
DataMapper.finalize
I'm not sure, though, what kinds of associations to add to Player and Referee. With belongs_to :user, multiple players can be associated with the same user, which doesn't make sense in my context. In RDBMS terms I guess what I want is a unique constraint on the foreign key in the Players and Referees tables.
How do I accomplish this in my DataMapper model? Do I have to perform the check myself in a validation?
There are different ways you could do this. Here's one option:
class User
include DataMapper::Resource
property :id, Serial
# Other properties...
has 1, :referee, :required => false
has 1, :player, :required => false
end
class Referee
include DataMapper::Resource
# DON'T include "property :id, Serial" here
# Other properties...
belongs_to :user, :key => true
end
class Player
include DataMapper::Resource
# DON'T include "property :id, Serial" here
# Other properties...
belongs_to :user, :key => true
end
Act on the referee/player models like:
u = User.create(...)
u.referee = Referee.create(...)
u.player = Player.create(...)
u.player.kick_ball() # or whatever you want to call
u.player.homeruns
u.referee.flag_play() # or whatever.
See if this works. I haven't actually tested it but it should be good.
The previous answer works other than :required => false is not recognized for has 1 properties.
It's also confusing because for has n properties, you can use new right on the property or otherwise treat it as a collection. In your example, you would be tempted to code
u = User.create ...
u.referee.create ...
But that fails in the case of has 1 because the property is a single value, which begins life as nil and so you have to use the method the previous answer indicates. Also, having to explicitly make the belongs_to association into the key is a little confusing.
It does seem to execute validations and have the right association actions (so u.save will also save the referred-to Referee). I just wish it were more consistent between has n and has 1.

Composite primary key is used only to access records but not save records in data_mapper

I have an app that tracks a player's progress over a fitness program. So every player has multiple weeks with the same :week_id
Week_id combined with the belongs_to relationship is the composite primary key for the Week record.
However, when I try to create two weeks with the same week_id that belong to different players, I get a "column week_id is not unique" error.
I feel like I am on the right track because when I want to fetch a week record, it tells me that I need two arguments to get it - the week_id and the player_id.
I am probably missing something simple here. I hope you can show me.
require "rubygems"
require "json"
require "sqlite3"
require "data_mapper"
require "bigdecimal"
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/prod.db")
DataMapper::Model.raise_on_save_failure = true
class Player
include DataMapper::Resource
property :name, String, :key => true
property :age, Integer
has n, :weeks
end
class Week
include DataMapper::Resource
property :week_id, Integer, :key => true
property :score, Integer
belongs_to :player, :key => true
end
DataMapper.finalize.auto_migrate!
#jack = Player.create(:name => "jack")
#jack.weeks.create(:week_id => 1)
#jill = Player.create(:name => "jill")
#jill.weeks.create(:week_id => 1)
Looks like you already came up with a solution, but I'll go ahead and post another answer for posterity, since I ran into the same issue.
It seems like declaring ':player_name' as a property that is a key as well as with the 'belongs_to' declaration, you end up with the same SQL to create the table, but it also recognizes that week_id is part of a composite key and does not need to be unique on its own. (It appears DataMapper has issues with composite keys split across 'property' and 'belongs_to' declarations.)
class Week
include DataMapper::Resource
property :week_id, Integer, :key => true
property :player_name, String, :key => true
property :score, Integer
belongs_to :player, :key => true
end
Results in the following SQL to create the table:
~ (0.000000) CREATE TABLE "weeks" ("week_id" INTEGER NOT NULL, "player_name" VARCHAR(50) NOT NULL, "score" INTEGER, PRIMARY KEY("week_id", "player_name"))
And your example code works without errors
So I ended up making a small change to my Week model like so
class Week
include DataMapper::Resource
property :id, String, :key => true
property :week_id, Integer
property :score, Integer
belongs_to :player
end
And whenever I create a new Player model I concatenate the foreign key and the week_id to create the id string
So my id string will look something like "jack1" for the first record for a player with an :id of "jack"
Not sure if this is the data_mapper way but it works.

DataMapper filter records by association count

With the following model, I'm looking for an efficient and straightforward way to return all of the Tasks that have 0 parent tasks (the top-level tasks, essentially). I'll eventually want to return things like 0 child tasks as well, so a general solution would be great. Is this possible using existing DataMapper functionality, or will I need to define a method to filter the results manually?
class Task
include DataMapper::Resource
property :id, Serial
property :name , String, :required => true
#Any link of type parent where this task is the target, represents a parent of this task
has n, :links_to_parents, 'Task::Link', :child_key => [ :target_id ], :type => 'Parent'
#Any link of type parent where this task is the source, represents a child of this task
has n, :links_to_children, 'Task::Link', :child_key => [ :source_id ], :type => 'Parent'
has n, :parents, self,
:through => :links_to_parents,
:via => :source
has n, :children, self,
:through => :links_to_children,
:via => :target
def add_parent(parent)
parents.concat(Array(parent))
save
self
end
def add_child(child)
children.concat(Array(child))
save
self
end
class Link
include DataMapper::Resource
storage_names[:default] = 'task_links'
belongs_to :source, 'Task', :key => true
belongs_to :target, 'Task', :key => true
property :type, String
end
end
I would like to be able to define a shared method on the Task class like:
def self.without_parents
#Code to return collection here
end
Thanks!
DataMapper falls down in these scenarios, since effectively what you're looking for is the LEFT JOIN query where everything on the right is NULL.
SELECT tasks.* FROM tasks LEFT JOIN parents_tasks ON parents_tasks.task_id = task.id WHERE parents_tasks.task_id IS NULL
You parents/children situation makes no different here, since they are both n:n mappings.
The most efficient you'll get with DataMapper alone (at least in version 1.x) is:
Task.all(:parents => nil)
Which will execute two queries. The first being a relatively simple SELECT from the n:n pivot table (WHERE task_id NOT NULL), and the second being a gigantic NOT IN for all of the id's returned in the first query... which is ultimately not what you're looking for.
I think you're going to have to write the SQL yourself unfortunately ;)
EDIT | https://github.com/datamapper/dm-ar-finders and it's find_by_sql method may be of interest. If field name abstraction is important to you, you can reference things like Model.storage_name and Model.some_property.field in your SQL.

Resources