You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to define my models before connecting to the database,
but sequel didn't allow me to do so. Then I put a trick there:
First make it connected to a in-memory SQLite database,
then defining the model (i.e. define schema and serialization)
Lastly reconnect to the real database appropriately.
I didn't know this won't work well in sequel before,
spending a lot of time digging, trying, and googling around.
Below is what I am doing right now to workaround that:
Sequel::Model.db=Sequel.connect('sqlite:memory')# dummy connectclassItem < Sequel::Modelplugin:schemaset_schemado# HACKprimary_key:id,:type=>'Serial',:auto_increment=>falseText:dataend# HACKcreate_table# this is needed! otherwise, the serialization would be# broken, due to different inclusion order or somethingplugin:serializationserialize_attributes[Yajl::Encoder.method(:encode),Yajl::Parser.method(:parse)],:data# connect to the real databasedefself.connectself.db=Sequel.connect('postgres://example.com/database')self.db.loggers << Logger.new($stdout)endend# somewhereItem.connect
I know that to support this, it might mean to rewrite a lot of
internal architecture inside sequel. But... I still hope if this
could be considered? Or, giving a better error message,
instead of crashing randomly?
Thank you!
The text was updated successfully, but these errors were encountered:
Sequel is this way by design. It needs to parse the database's schema at model creation time. Why is it a problem to connect to the database before loading your model classes?
You can work around this issue by using anonymous classes:
Item = Class.new(Sequel::Model)
Then just don't call set_dataset/dataset= until after the database connection has been made. You are responsible for picking up the pieces when this breaks.
Also, just for future reference, feature requests should be sent to the Google Group, the GitHub issue tracker should only be used for bugs/pull requests.
Regarding why it is a problem connecting to a database before loading
model classes,
it is not really a problem, but it would be more flexible if it is
allowed to connect to the
database whenever really needed.
For example, then I could put utilities methods inside model classes
and use them without connecting to a database if I don't want to.
I want to define my models before connecting to the database,
but sequel didn't allow me to do so. Then I put a trick there:
First make it connected to a in-memory SQLite database,
then defining the model (i.e. define schema and serialization)
Lastly reconnect to the real database appropriately.
I didn't know this won't work well in sequel before,
spending a lot of time digging, trying, and googling around.
Below is what I am doing right now to workaround that:
I know that to support this, it might mean to rewrite a lot of
internal architecture inside sequel. But... I still hope if this
could be considered? Or, giving a better error message,
instead of crashing randomly?
Thank you!
The text was updated successfully, but these errors were encountered: