tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

How to translate MySQL code with two primary keys to Tortoise?

Open Vyacguru opened this issue 1 year ago • 0 comments

I have this in my schema generation code:

--
-- Table structure for table `oc_location`
--

DROP TABLE IF EXISTS `oc_length_class_description`;
CREATE TABLE `oc_length_class_description` (
  `length_class_id` int(11) NOT NULL,
  `language_id` int(11) NOT NULL,
  `title` varchar(32) NOT NULL,
  `unit` varchar(4) NOT NULL,
  PRIMARY KEY (`length_class_id`,`language_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

How to rewrite this to Tortoise Model? Like this?

class LengthClassDescription(Model):
    length_class_id = IntField(null=False, pk=True, validators=[MaxLengthValidator(11)])
    language_id = IntField(null=False, pk=True, validators=[MaxLengthValidator(11)])
    title = CharField(max_length=32, null=False)
    unit = CharField(max_length=4, null=False)

    class Meta:
        table = f"{database_prefix}length_class_description"

Or?

class LengthClassDescription(Model):
    length_class_id = IntField(null=False, validators=[MaxLengthValidator(11)])
    language_id = IntField(null=False, validators=[MaxLengthValidator(11)])
    title = CharField(max_length=32, null=False)
    unit = CharField(max_length=4, null=False)

    class Meta:
        table = f"{database_prefix}length_class_description"
        unique_together = (("length_class_id", "language_id"),)

Vyacguru avatar Jan 15 '24 21:01 Vyacguru