Home  >  Q&A  >  body text

How to create a model when the database table contains hyphens?

<p>I have tables with hyphens in my database, for example "user-cars". I can't change the name to "user_cars". Is there a way to name the model "user_cars" but have it reference the "user-cars" table? I tried <code>@@map("user_cars")</code> but it didn't work. </p>
P粉007288593P粉007288593438 days ago527

reply all(1)I'll reply

  • P粉520545753

    P粉5205457532023-09-01 12:46:41

    According to the documentation, you need to map the model to the underlying table name. You tried user_cars, but did you try user-cars? If your table name is user-cars, then you should map it to your model like this:

    model UserCar {
      // Fields
    
      @@map("user-cars")
    }

    reply
    0
  • Cancelreply