0

Is there a way for ActiveRecord to capture all the data base's fields, including the Pks and FKs? Like what the ORM DBIx::Class script does on Catalyst?

Here's a brief description of that:

Generate the model using the Catalyst "_create.pl" script

script:

$ rm lib/MyApp/Model/DB.pm 

$ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ create=static components=TimeStamp,PassphraseColumn \ 'dbi:Pg:dbname=catappdb' 'catappuser' 'catalyst' '{ AutoCommit => 1 }'

More details on DBIx::Class

So, I need to be able to create my DB using sql and then just run a script so that Rail's ORM ActiveRecord maps all the tables and relations.

(sorry for my bad english :3 )

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
xCanox
  • 1
  • 2

1 Answers1

0

You have to declare your relationships in your ActiveRecord model in an analogous way that you have to declare them in DBIx::Class. Neither DBIx::Class nor ActiveRecord know anything about your FKs if you don't declare them in the Model class.

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
  • The asker is referring to the ability to generate the base ORM files and relationships base don the existing DB schema. This is a fairly common ORM feature. E.g. DBIx::Class allows creating the DB schema from the defined ORM class files, or the reverse, creating the ORM class files by reading the DB Schema and creating the class files from it, with relationships defined by following foreign key constraints. – kbenson Apr 11 '14 at 16:38
  • I Don't recall DBIC::Schema::Loader generating relationship statements for me. That's not to say that it doesn't just hasn't for me when I used it. Rails generators have never added relationships to my models – Len Jaffe Apr 11 '14 at 16:58
  • In my specific instance (Percona MySQL using InnoDB), my preferred method of dealing with relationships is creating the foreign key constraints, and then just re-running dbicdump, which I do every week or two, so I know it works. It may be support is not fully implemented for all RDBMS. – kbenson Apr 11 '14 at 20:22
  • Granted. But the question is active record, and active record biases towards maintaining relationships in the application, rather than via FK. This is due to the fact that if FKs don't tend to span servers, so if you shard your data, a row on one shard cannot usually have related rows on any other shard. Grouping related data is often good, but it also constrains parallelism. – Len Jaffe Apr 15 '14 at 13:44
  • I was merely commenting on your assertion that "Neither DBIx::Class nor ActiveRecord know anything about your FKs if you don't declare them in the Model class", with more specific information on the DBIx::Class side. DBIx::Class also allows manual relationship definition, or a combination of the two (which I use). Generated files contain `# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:PqQ8s2YshkI0T6s1Avqnbw` after which you can put your own definition data, methods, etc. I agree that being forced to rely on FK for relationships would be too constraining. – kbenson Apr 23 '14 at 20:34
  • after you generate your model, your foreign key relationship data is in your model. It is not dug out of the data dictionary unless you use schema loader at run-time. In general, you don't need a database that does not define FKs. If you're big enough to shard your db, then congrats, you're elite. – Len Jaffe Apr 23 '14 at 20:45
  • The original question was about automatically generating the model from the schema. Your answer implies that DBIx::Class can't generate the model because it doesn't know about FKs you haven't set yourself, which _in light of the question_ I interpreted as somewhat misinformed. I'm not sure where the elitism stuff comes in, I was just _agreeing with your prior comment regarding automatic schema generation of sharded data_ and pointing out a feature. – kbenson Apr 23 '14 at 20:58
  • You are correct, except I've never seen active record capture FK info at generation time. – Len Jaffe Apr 24 '14 at 13:24