我需要创建一个包含以下表格的数据库:School、Address、Years、Types和Results。每个School只能有一个 Address和多个 Years,每个Year必须有多个 Types,并且每年每个Type只能有一个Results表。
我的 DataMapper 模型正在下降:
class School
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 225, :required => true
property :type, String
has n, :years
has 1, :address
has n, :types, :through => :years
has n, :visits
end
class Year
include DataMapper::Resource
property :id, Serial
property :year, Integer
has n, :types
has n, :schools
end
class Result
include DataMapper::Resource
property :id, Serial
property :subject, String, :length => 225
property :total, Integer
property :range_1, Integer
property :range_2, Integer
property :range_3, Integer
property :range_4, Integer
property :range_5, Integer
property :range_6, Integer
property :range_7, Integer
property :range_8, Integer
property :range_9, Integer
property :range_10, Integer
belongs_to :type
end
class Type
include DataMapper::Resource
property :id, Serial
property :name, String, :length => 225, :required => true
has n, :results
has n, :years
has n, :schools, :through => :years
end
class Address
include DataMapper::Resource
property :id, Serial
property :Village, String
property :City, String
property :District, String
property :State, String
belongs_to :school
end
我找不到更好的方法来声明表之间的关联。也许你可以提出更好的建议?
还有,如何选择取决于学校和年份的具体结果?