12

In my (non-Rails) app, I'm trying to define a Sequel model:

class Foo < Sequel::Model
end

When I run my app, I'm getting the error:

No database associated with Sequel::Model: 
have you called Sequel.connect or Sequel::Model.db= ? (Sequel::Error)

In fact, I have not called connect, because the 'require Foo' is happening before my database code runs.

Of course, I could switch things around so that the require is done after the DB connects, but is there another option? Currently I have all my app's 'require' statements in one file and it would be nice not to have to break that for these model class files.

1 Answer 1

11

By design, Sequel requires the database connection be set up before model class definition, since it parses the database schema on model class creation. So you should set up your initialization code to connect to the database first.

2
  • 16
    I've just read this and I have to disagree on the design decision. It's terrible to have to connect to the database before loading a gem that includes sequel models.
    – josemota
    Feb 5, 2015 at 16:14
  • 2
    Only other way to do this is have an explicit schema definition within the model or schema.rb file which is how I think activerecord does it. Apr 26, 2016 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.