[Nitro] og troubles

Francesco Lunelli francesco.l at ymir.it
Wed Nov 16 13:44:23 EST 2005


Michael Fellinger ha scritto:

>Hi Francesco,
>
>It would be of great interest to me (and all the others i guess) what your 
>run.rb and og-model looks like.
>The issue with psql-database-creation is known (at least to me) but not fixed 
>yet (i created the database once and now morphing is working well for me)
>Please provide us with the code you tested so we can add it to the testcases 
>and aim on fixing and of course tell you what is wrong with it.
>
>thx in advance
>manveru
>
>  
>
The code I am testing is the example you anc find in examples directory 
in og

/usr/lib/ruby/gems/1.8/gems/og-0.24.0/examples

I paste it a the end of my mail.
What I find very strange is that the same code is working perfectly in 
my Kubuntu amd64 but gives me errors in Debian amd64 and in kubuntu 
i386. I tested it with postgresql 7.4 and 8.0

Running it after creating database test maually gives me this error:

D, [2005-11-16T19:41:05.671950 #11730] DEBUG -- : Og manageable classes: 
[Part, UserComment, ArticleComment, Category, Article, U       ser, Comment]
NOTICE:  CREATE TABLE will create implicit sequence "ogpart_oid_seq" for 
"serial" column "ogpart.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogpart_pkey" for table "ogpart"
I, [2005-11-16T19:41:05.744207 #11730]  INFO -- : Created table 'ogpart'.
NOTICE:  CREATE TABLE will create implicit sequence "ogcomment_oid_seq" 
for "serial" column "ogcomment.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogcomment_pkey" for table "ogcomment"
I, [2005-11-16T19:41:05.811032 #11730]  INFO -- : Created table 'ogcomment'.
NOTICE:  CREATE TABLE will create implicit sequence 
"ogusercomment_oid_seq" for "serial" column "ogusercomment.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogusercomment_pkey" for table "ogusercomment"
I, [2005-11-16T19:41:05.884816 #11730]  INFO -- : Created table 
'ogusercomment'.
NOTICE:  CREATE TABLE will create implicit sequence 
"ogarticlecomment_oid_seq" for "serial" column "ogarticlecomment.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogarticlecomment_pkey" for table "ogarticlecomment"
I, [2005-11-16T19:41:05.993305 #11730]  INFO -- : Created table 
'ogarticlecomment'.
NOTICE:  CREATE TABLE will create implicit sequence "ogcategory_oid_seq" 
for "serial" column "ogcategory.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogcategory_pkey" for table "ogcategory"
I, [2005-11-16T19:41:06.071713 #11730]  INFO -- : Created table 
'ogcategory'.
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogj_article_category_pkey" for table "ogj_article_category"
D, [2005-11-16T19:41:06.080041 #11730] DEBUG -- : Created jointable 
'ogj_article_category'.
NOTICE:  CREATE TABLE will create implicit sequence "ogarticle_oid_seq" 
for "serial" column "ogarticle.oid"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogarticle_pkey" for table "ogarticle"
I, [2005-11-16T19:41:06.222786 #11730]  INFO -- : Created table 'ogarticle'.
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"ogj_article_category_pkey" for table "ogj_article_category"
/usr/local/lib/site_ruby/1.8/og/store/psql.rb:222:in `exec': ERROR:  la 
relazione "ogj_article_category" esiste già (PGError)
        from /usr/local/lib/site_ruby/1.8/og/store/psql.rb:222:in 
`create_table'
        from /usr/local/lib/site_ruby/1.8/og/store/psql.rb:221:in `each'
        from /usr/local/lib/site_ruby/1.8/og/store/psql.rb:221:in 
`create_table'
        from /usr/local/lib/site_ruby/1.8/og/store/psql.rb:219:in 
`create_table'
        from (eval):6:in `og_create_schema'
        from /usr/local/lib/site_ruby/1.8/og/store/sql.rb:295:in `enchant'
        from /usr/local/lib/site_ruby/1.8/og/store/psql.rb:142:in `enchant'
        from /usr/local/lib/site_ruby/1.8/og/manager.rb:120:in `manage'
        from /usr/local/lib/site_ruby/1.8/og/manager.rb:175:in 
`manage_classes'
        from /usr/local/lib/site_ruby/1.8/og/manager.rb:175:in `each'
        from /usr/local/lib/site_ruby/1.8/og/manager.rb:175:in 
`manage_classes'
        from /usr/local/lib/site_ruby/1.8/og.rb:117:in `setup'
        from run.rb:147





------------------------------- Example code-----------------

# A simple example to demonstrate the Og library.

require 'og'

# Full debug information.

$DBG = true

# A child class.

class Comment
  property :body, String
 
  def initialize(body = nil)
    @body = body
  end
 
  def to_s
    return @body
  end
end

# = A Parent class.

class User
  property :name, String, :uniq => true
  has_many :comments, UserComment
 
  def initialize(name = nil)
    @name = name
  end
 
  def to_s
    return @name
  end
end


# A parent class.

class Article
  property :title, String
  property :body, String

  # override the default O->R mapping

  property :level, Fixnum, :sql => "smallint DEFAULT 1"

  # store a Ruby Hash in the Database. YAML
  # is used for serializing the attribute.
  # no need to define the class, but you can if you want.

  property :options

  # exactly like the standard Ruby attr creates only the reader.

  prop :create_time, Time
 
  # define comment relation:

  has_many :comments, ArticleComment
 
  has_many :parts, Part

  # many to many relation.
 
  many_to_many Category
 
  # define author relation:

  belongs_to :author, User
 
  # this attribute is NOT stored in the db.

  attr_accessor :other_options
 
  # Managed object constructors with no args, take *args
  # as parameter to allow for Mixin chaining.

  def initialize(title = nil, body = nil)
    @title, @body = title, body
    @create_time = Time.now
    @options = {}
    @other_options = {}
  end
 
  def to_s
    return "#@title: #@body"
  end
end

# A parent class.

class Category
  property :title, String
  property :body, String

  # define a 'many to many' relation. 

  many_to_many Article
 
  def initialize(title = nil)
    @title = title
  end
end


# Article comment.

class ArticleComment < Comment
  belongs_to Article
end

# User comment.

class UserComment < Comment
  belongs_to :author, User
end

# Another child class.

class Part
  property :name, String
  belongs_to Article
 
  def initialize(name = nil)
    @name = name
  end
 
  def to_s
    return @name
  end
end

# Og configuration.

config = {
  :destroy => false, # destroy table created from earlier runs.
  :store => 'psql',
  :name => 'test',
  :user => "franz",
  :password => "franz"
}

# Initialize Og

db = Og.setup(config)

# Create some articles

a1 = Article.new('Title1', 'Body1')
a1.save

# shortcut

a2 = Article.create('Title2', 'Body2')

puts "\n\n"
puts "* Get and print all articles:"
articles = Article.all
articles.each { |a| puts a }

# Create some comments

c1 = ArticleComment.new('Comment 1')
c1.article = a1
c1.save

c2 = ArticleComment.new('Comment 2')
# alternative way to set the parent.
c2.article_oid = a1.oid
# an alternative way to save
db.store << c2

# an alternative (easier and cooler) way to add children in a
# has_many relation:
c3 = ArticleComment.new('Comment 3')
# add_comment is automatically added by Og.
a1.comments << c3

puts "\n\n"
puts "* Print all all comments for article 1:"
a1.comments.each { |c| puts c }

# Most Og commands allow you to fine-tune the low level
# SQL code by passing extra_sql parameters, here is an
# example
puts "\n\n"
puts "* comments with sql finetunings:"
# use a standard SQL limit clause
a1.comments(:limit => 2).each { |c| puts c }


# Change a managed object
a1.title = 'Changed Title'
# Og knows that this is a managed object and executes
# an SQL UPDATE instead of an SQL INSERT
a1.save!

puts "\n\n"
Article.all.each { |a| puts a }

# The previous command updates the whole object. It is used
# when there are many updates or you dont care about speed.
# You can also update specific fields
a2.title = 'A specific title'
a2.update(:properties => [:title])

puts "\n\n"
Article.all.each { |a| puts a }

# delete an object
puts '-----------------1'
ArticleComment.delete(c3)
puts '-----------------2'

puts "\n\n"
ArticleComment.all.each { |a| puts a }


# Serialize a hash
a1.options = { :k1 => 'val1', :k2 => 'val2' }
a1.save!

# lookup an object
article = Article[a1.oid]

puts "\n\n"
puts article.options.inspect

u = User.new('gmosx')
u.save!

article = Article[1]
# you can also lookup by the name property.
article.author = User.find_by_name('gmosx') 
article.save!

part = Part.new('admin')
part.article = article
part.save!

article.parts.each { |pa| puts pa }

puts "\n\n"
puts '---'

c1 = Category.create('Category1')
c2 = Category.create('Category2')

article.categories << c1
article.categories << c2

puts '---'

article.categories.each { |c| puts c.title }

puts '---'

c2.articles.each { |a| puts a.title }

article.categories.delete(c1)

puts '---'

article.categories.each { |c| puts c.title }

# create and save the article in one step.
article = Article.create('title', 'body')

puts '--', article.oid

-- 
Francesco Lunelli
Ymir s.r.l.
Viale Verona 190/11
38100 Trento





More information about the Nitro-general mailing list