-
Hi, The problem I have is that I cannot use a seed because the model needs the foreign key: This is the error output: insert into `tags` (`category_id`, `name`, `slug`, `uid`) values (0, 'amet consequatur itaque aut', 'amet-consequatur-itaque-aut', 'e46e5fc4-bcf7-4629-9d2b-aab1c625f798') - ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`TeTraqueo`.`tags`, CONSTRAINT `tags_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`uid`)) The problem is that category_id (that is an uuid field) has 0 value when in reality it must have the category primary key as value This is my factory: import Factory from '@ioc:Adonis/Lucid/Factory'
import Category from 'App/Models/Category'
import Tag from 'App/Models/Tag'
import _ from "lodash"
export const TagFactory = Factory
.define(Tag, ({ faker }) => {
return {
name: faker.lorem.words(_.random(1, 4, false)),
}
})
.build()
export const CategoryFactory = Factory
.define(Category, ({ faker }) => {
return {
name: faker.company.companyName(),
}
})
.relation('tags', () => TagFactory)
.build() And here is the seed: public async run() {
const category = await CategoryFactory.with("tags",3).create()
} I have two tables: categories and tags. One category has many tags and one Tag belongs to a category. Like this: Category Model: export default class Category extends BaseModel {
@beforeCreate()
public static async addUidHook(category: Category) {
category.uid = uuidv4();
}
public static table = "categories";
@column({ isPrimary: true })
public uid: string;
@hasMany(() => Tag,{
localKey:"uid",
foreignKey:"categoryId",
})
public tags: HasMany<typeof Tag>
....
} Tag Model: public static table = "tags";
export default class Tag extends BaseModel {
@beforeCreate()
public static async addUidHook(tag: Tag) {
tag.uid = uuidv4();
}
@column()
public categoryId: string;
@belongsTo(() => category,{
localKey: 'categoryId',
foreignKey: 'uid',
})
public category: BelongsTo<typeof category>
...
} Here is the category schema: public async up () {
this.schema.createTable(this.tableName, (table) => {
table.uuid("uid").primary().unique().notNullable(); Here is the tag schema export default class Tags extends BaseSchema {
protected tableName = 'tags'
public async up () {
this.schema.createTable(this.tableName, (table) => {
table.uuid("uid").primary().unique().notNullable();
table.uuid('category_id').references('uid').inTable('categories').notNullable()
....
} Do you have any clue what it could be? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok, I managed to fix it, to be honest I don't know if this is the best answer but at least it works: I added a fake uuid generator to the factory: import { v4 as uuidv4 } from "uuid";
const myUid= uuidv4()
export const TagFactory = Factory
.define(Tag, ({ faker }) => {
return {
name:faker.random.word() + faker.lorem.word() + "" + faker.random.number(99),
category_id: myUid,
}
})
.build()
export const CategoryFactory = Factory
.define(Category, ({ faker }) => {
return {
name: faker.company.companyName(),
uid:myUid
}
})
.relation('tags', () => TagFactory)
.build() I also replaced the old seeder with this: await CategoryFactory.create()
await TagFactory.createMany(50) and in both category and tag model i made a conditional to check if there is an uid property assigned: @beforeCreate()
public static async addUidHook(category: Category) {
if(category.uid !== undefined) return
category.uid = uuidv4();
}
|
Beta Was this translation helpful? Give feedback.
Ok, I managed to fix it, to be honest I don't know if this is the best answer but at least it works:
I added a fake uuid generator to the factory:
I also replaced the old seeder with this: