Git Product home page Git Product logo

Comments (22)

schmurfy avatar schmurfy commented on August 28, 2024 1

Thanks this got me a step closer to a working start, I now have this:

require 'rom'
require 'rom-repository'

rom = ROM.container(:sql, 'sqlite:test.db') do |config|

  class LibraryCollections < ROM::Relation[:sql]
    schema('AgLibraryCollection', infer: true)  
  end
  
  config.register_relation(LibraryCollections)
end

class LibraryCollectionRepo < ROM::Repository[:library_collections]  
end

repo = LibraryCollectionRepo.new(rom)
p repo.library_collections.to_a

But it fails on line 8 with:

sequel-4.42.1/lib/sequel/database/query.rb:162:in `schema': schema parsing returned no columns, table "AgLibraryCollection" probably doesn't exist (Sequel::Error)
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:41:in `call'
	from rom-sql-0.9.1/lib/rom/sql/relation.rb:47:in `block (2 levels) in inherited'
	from rom-2.0.2/lib/rom/schema.rb:83:in `finalize!'
	from rom-sql-0.9.1/lib/rom/sql/schema.rb:24:in `finalize!'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:53:in `build_relation'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:25:in `block (2 levels) in run!'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:24:in `each'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:24:in `block in run!'
	from rom-2.0.2/lib/rom/relation_registry.rb:6:in `initialize'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:23:in `new'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:23:in `run!'
	from rom-2.0.2/lib/rom/setup/finalize.rb:93:in `load_relations'
	from rom-2.0.2/lib/rom/setup/finalize.rb:62:in `run!'
	from rom-2.0.2/lib/rom/create_container.rb:35:in `finalize'
	from rom-2.0.2/lib/rom/create_container.rb:13:in `initialize'
	from rom-2.0.2/lib/rom/create_container.rb:54:in `initialize'
	from rom-2.0.2/lib/rom/create_container.rb:59:in `new'
	from rom-2.0.2/lib/rom/create_container.rb:59:in `container'

I know the table does exists:

$ sqlite3 test.db "SELECT COUNT(*) FROM AgLibraryCollection"
4

I have been following rom for a while and I like the design, the current documentation is a great start but you are right, providing enough data without overloading the reader might prove difficult :)

I always prefer to have something working quickly even if I don't get all the principles behind it and then starting from that I can tweak things and see how it reacts and change my result, after that I can read more about how things work. I think maintaining a list of quickstarts instead of one is a good solution, you already have the read/write case, adding the read case might help others too.

from rom-sql.

solnic avatar solnic commented on August 28, 2024 1

Oh, I didn't notice your connection URI is not right, it should be a valid uri with path to your db file so something like sqlite:///path/to/your/test.db

from rom-sql.

solnic avatar solnic commented on August 28, 2024

Here's a quick start: http://rom-rb.org/learn/repositories/quick-start/

Here what I managed to write (and does not work):

What doesn't work exactly? Any backtrace?

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

The error I get from the code above is:

rom-2.0.2/lib/rom/relation.rb:90:in `each': undefined method `each' for #<ROM::Container:0x007ff7f2046e78> (NoMethodError)
	from rom-2.0.2/lib/rom/relation.rb:119:in `each'
	from rom-2.0.2/lib/rom/relation.rb:119:in `to_a'
	from rom-2.0.2/lib/rom/relation.rb:119:in `to_a'
	from app.rb:26:in `<main>'

I did not link it because I am pretty sure I am just doing something wrong.

I saw the quickstart and read the documentation but it is really fragemented, I don't yet understand how to piece things together to have a working base, from what I got I need to:

  • create a container which is the database connection
  • create one or more relations, create_table as used in the example does not work for I supposed since I don't want to alter the existing schema, just use it that's why I tried to create it myself
  • create the repository, maybe ? I tried but also failed:
class LibraryCollection < ROM::Relation[:sql]
end

class LibraryCollectionRepo < ROM::Repository[???]
end

# for the question marks I tried :library_collection but it says it does not know the relation, also tried to put the class name itself "LibraryCollection" but did not work either
# => rom-support-2.0.0/lib/rom/support/registry.rb:33:in `block in fetch': :library_collection doesn't exist in ROM::RelationRegistry registry (ROM::Registry::ElementNotFoundError)

If you could provide a fully working starting point it would really help, and no the quickstart is not that for me, it shows one usecase but this is not mine and I can't manage to translate it which is really frustrating :(

from rom-sql.

solnic avatar solnic commented on August 28, 2024

If you just want to connect to an existing database and define an explicit relation class then you want this:

require 'rom'
require 'rom-repository'

rom = ROM.container(:sql, 'sqlite:test.db') do |config|
  # this assumes you have a table called :library_collections
  class LibraryCollections < ROM::Relation[:sql]
    schema(infer: true)
  end
  
  config.register_relation(LibraryCollections)
end

class LibraryCollectionRepo < ROM::Repository[:library_collections]
end

repo = LibraryCollectionRepo.new(rom)

p repo.library_collections.to_a

it shows one usecase but this is not mine and I can't manage to translate it which is really frustrating :(

We'll be working on new/more docs/guides. It's extremely difficult to document rom due to its broad functionality, and very little time, it's slowly progressing in a better direction. I'll add quick start section for connecting to an existing database too.

Thanks for feedback. Let me know if you need more help, I hope the example I showed will work for you.

from rom-sql.

solnic avatar solnic commented on August 28, 2024

If your table is called differently than the name you'd like to use, then you need to provide it as a symbol and you can alias it like that:

  class LibraryCollections < ROM::Relation[:sql]
    register_as :library_collections
    schema(:AgLibraryCollection, infer: true)
  end

if you switch to rom-sql 1.0.0.beta2 you can also do this:

  class LibraryCollections < ROM::Relation[:sql]
    schema(:AgLibraryCollection, infer: true, as: :library_collections)
  end

I always prefer to have something working quickly even if I don't get all the principles behind it and then starting from that I can tweak things and see how it reacts and change my result, after that I can read more about how things work.

Yep I'm the same, I'll definitely add short intros like that. It should probably go to a different section of the site, maybe "Guides" where this type of docs fit better. "Learn" section is meant to be a detailed documentation about all rom components with examples how to use various APIs.

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

I tried with a symbol and it still did not work (same error as before), I dug around a bit trying to pass Sequel a logger and found how with config.gateways[:default].use_logger(logger) and it shows that the code executed this:

PRAGMA foreign_keys = 1;
PRAGMA case_sensitive_like = 1;
SELECT sqlite_version();
SELECT * FROM `sqlite_master` WHERE ((`name` != 'sqlite_sequence') AND (`type` = 'table'));
PRAGMA table_info('AgLibraryCollection');

I tried to execute the last command, and... it works:

$ sqlite3 test.db "PRAGMA table_info('AgLibraryCollection')"
0|id_local|INTEGER|0||1
1|creationId||1|''|0
2|genealogy||1|''|0
3|imageCount||0||0
4|name||1|''|0
5|parent|INTEGER|0||0
6|systemOnly||1|''|0

This gets weirder and weirder :o

from rom-sql.

Kukunin avatar Kukunin commented on August 28, 2024

also, you can take a look here: https://github.com/rom-rb/rom-repository/blob/master/examples/sql.rb. One advice with rom-rb, it's always useful to check the tests

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

I find highly unsettling that you can pass an invalid url without issue and the rest of the queries will just happily raise meaningless errors, I suppose it comes directly from sequel and is not in rom but stil annoying.

I fixed the path and my container line now looks like this: rom = ROM.container(:sql, 'sqlite://./test.db') do |config|, I got a new error :p

rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:69:in `block in map_type': Cannot find corresponding type for  (ROM::SQL::UnknownDBTypeError)
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:68:in `fetch'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:68:in `map_type'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:56:in `build_type'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:45:in `block in call'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:44:in `each'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:44:in `each_with_object'
	from rom-sql-0.9.1/lib/rom/sql/schema/inferrer.rb:44:in `call'
	from rom-sql-0.9.1/lib/rom/sql/relation.rb:47:in `block (2 levels) in inherited'
	from rom-2.0.2/lib/rom/schema.rb:83:in `finalize!'
	from rom-sql-0.9.1/lib/rom/sql/schema.rb:24:in `finalize!'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:53:in `build_relation'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:25:in `block (2 levels) in run!'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:24:in `each'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:24:in `block in run!'
	from rom-2.0.2/lib/rom/relation_registry.rb:6:in `initialize'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:23:in `new'
	from rom-2.0.2/lib/rom/setup/finalize/finalize_relations.rb:23:in `run!'
	from rom-2.0.2/lib/rom/setup/finalize.rb:93:in `load_relations'
	from rom-2.0.2/lib/rom/setup/finalize.rb:62:in `run!'
	from rom-2.0.2/lib/rom/create_container.rb:35:in `finalize'
	from rom-2.0.2/lib/rom/create_container.rb:13:in `initialize'
	from rom-2.0.2/lib/rom/create_container.rb:54:in `initialize'
	from rom-2.0.2/lib/rom/create_container.rb:59:in `new'
	from rom-2.0.2/lib/rom/create_container.rb:59:in `container'
	from app.rb:10:in `<main>'

@Kukunin I looked at the tests files but it really does not give much more information than what is in the quickstart in the documentation, my comments above also applies to it: it create a memory database, I am opening an existing one, it creates a new schema where I am using an existing one.

from rom-sql.

solnic avatar solnic commented on August 28, 2024

I find highly unsettling that you can pass an invalid url without issue and the rest of the queries will just happily raise meaningless errors

It's hard to tell but maybe it's a bug in sequel, I would expect to see an error about invalid URI. I just checked and it seems like it created an in-memory sqlite database, just because there's no path in that URI. Something worth reporting to Jeremy I think.

I got a new error :p

Can you try installing beta version of rom and rom-sql and try again? We improved inferrer a lot so it might work. If it doesn't please paste here what rom.gateways[:default].connection.schema(:AgLibraryCollection) returns.

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

I just tried with latest beta:

gem 'rom',              '~> 3.0.0.beta3'
gem 'rom-repository',   '~> 1.0.0.beta2'
gem 'rom-sql',          '~> 1.0.0.beta3'

my script got a little further it seems:

rom-3.0.0.beta3/lib/rom/schema.rb:271:in `finalize!': missing attributes in ROM::Relation::Name(AgLibraryCollection) schema: :creationId, :genealogy, :imageCount, :name, :systemOnly (ROM::Schema::MissingAttributesError)
	from rom-sql-1.0.0.beta3/lib/rom/sql/schema.rb:85:in `finalize!'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:54:in `build_relation'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:25:in `block (2 levels) in run!'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:24:in `each'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:24:in `block in run!'
	from rom-3.0.0.beta3/lib/rom/relation_registry.rb:6:in `initialize'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:23:in `new'
	from rom-3.0.0.beta3/lib/rom/setup/finalize/finalize_relations.rb:23:in `run!'
	from rom-3.0.0.beta3/lib/rom/setup/finalize.rb:93:in `load_relations'
	from rom-3.0.0.beta3/lib/rom/setup/finalize.rb:62:in `run!'
	from rom-3.0.0.beta3/lib/rom/create_container.rb:33:in `finalize'
	from rom-3.0.0.beta3/lib/rom/create_container.rb:11:in `initialize'
	from rom-3.0.0.beta3/lib/rom/create_container.rb:52:in `initialize'
	from rom-3.0.0.beta3/lib/rom/create_container.rb:57:in `new'
	from rom-3.0.0.beta3/lib/rom/create_container.rb:57:in `container'
	from app.rb:11:in `<main>'

I don't understand the error, what does missing attributes means in that case ?

PS: As a sidenote if I do require "rom" an exception is raised complaining about uninitialized constant Forwardable, I had to add require 'forwardable' just before it to make it work.

from rom-sql.

solnic avatar solnic commented on August 28, 2024

my script got a little further it seems:

OK cool, please paste what this returns:

rom.gateways[:default].connection.schema(:AgLibraryCollection)

As a sidenote if I do require "rom" an exception is raised complaining about uninitialized constant Forwardable

Oh nice catch, I'll fix that right away

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

Here is the result:

[
  [:id_local, {
    :allow_null=>true,
    :default=>nil,
    :db_type=>"INTEGER",
    :primary_key=>true,
    :auto_increment=>true,
    :type=>:integer,
    :ruby_default=>nil
  }],
  [:creationId, {
    :allow_null=>false,
    :default=>"''",
    :db_type=>"",
    :primary_key=>false,
    :type=>nil,
    :ruby_default=>nil
  }],
  [:genealogy, {
    :allow_null=>false,
    :default=>"''",
    :db_type=>"",
    :primary_key=>false,
    :type=>nil,
    :ruby_default=>nil
  }],
  [:imageCount, {
    :allow_null=>true,
    :default=>nil,
    :db_type=>"",
    :primary_key=>false,
    :type=>nil,
    :ruby_default=>nil
  }],
  [:name, {
    :allow_null=>false,
    :default=>"''",
    :db_type=>"",
    :primary_key=>false,
    :type=>nil,
    :ruby_default=>nil
  }],
  [:parent, {
    :allow_null=>true,
    :default=>nil,
    :db_type=>"INTEGER",
    :primary_key=>false,
    :type=>:integer,
    :ruby_default=>nil
  }],
  [:systemOnly, {
    :allow_null=>false,
    :default=>"''",
    :db_type=>"",
    :primary_key=>false,
    :type=>nil,
    :ruby_default=>nil
  }]
]

from rom-sql.

flash-gordon avatar flash-gordon commented on August 28, 2024

from rom-sql.

solnic avatar solnic commented on August 28, 2024

@flash-gordon ^^ any clue why db_type is set to an empty string?

@schmurfy there's no way to infer these attributes given that sequel returns empty strings for db_type property. Can you show me how this schema was created? For the time being you can define attributes yourself explicitly in the schema block:

schema(:AgLibraryCollection, infer: true) do
  attribute :creationId, Types::Int
  attribute :imageCount, Types::Int
  attribute :name, Types::String
  attribute :genealogy, Types::String # not sure if you want a string here
  attribute :systemOnly, Types::String # same here, not sure if that's supposed to be a string
end

from rom-sql.

solnic avatar solnic commented on August 28, 2024

@flash-gordon yeah me too, I mean if you look at this:

$ sqlite3 test.db "PRAGMA table_info('AgLibraryCollection')"
0|id_local|INTEGER|0||1
1|creationId||1|''|0
2|genealogy||1|''|0
3|imageCount||0||0
4|name||1|''|0
5|parent|INTEGER|0||0
6|systemOnly||1|''|0

then it's indeed empty, only id_local has INTEGER set the rest is just empty...should we default to String in such case? I've no idea what sqlite is doing under the hood.

UPDATE: and parent has INTEGER

from rom-sql.

flash-gordon avatar flash-gordon commented on August 28, 2024

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

here is the schema returned using the cli utility and the .schema command:

CREATE TABLE AgLibraryCollection (
    id_local INTEGER PRIMARY KEY,
    creationId NOT NULL DEFAULT '',
    genealogy NOT NULL DEFAULT '',
    imageCount,
    name NOT NULL DEFAULT '',
    parent INTEGER,
    systemOnly NOT NULL DEFAULT ''
);

if I read that well there is no type defined for most of the columns xD

An interesting point here is that my Sequel did manage to infer the schema, maybe you should use string as default if nothing else if defined.

from rom-sql.

solnic avatar solnic commented on August 28, 2024

@flash-gordon @schmurfy wdyt about adding Types::Object for "who knows what" types? Seems appropriate in this weird sqlite case

from rom-sql.

flash-gordon avatar flash-gordon commented on August 28, 2024

@solnic yes, that's one option. The other is to return strings, but I'd 👍 for Object type

from rom-sql.

schmurfy avatar schmurfy commented on August 28, 2024

What would the Object solution means, what would it returns ?

from rom-sql.

solnic avatar solnic commented on August 28, 2024

@schmurfy it would just set attribute primitive type to Object and that's it, since we don't know what it is exactly then it's the only solution we can come up with

from rom-sql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.