Uh oh!
There was an error while loading. Please reload this page.
feat: including relationships in migrations - #144
Conversation
| <%if (props.ref) { | ||
| props.ref.forEach(function(link){ | ||
| if (link.relationship === 'One-to-One') { %> | ||
| // table.integer('<%-link.columnName %>').unsigned().nullable() |
There was a problem hiding this comment.
the idea are to not create a migration that does not work, due to not has an order of migrations when we write the files. These relationships throw errors due the dependence dont exist yet ...
jhomarolo
commented
Jul 5, 2022
endersoncosta
commented
Jul 6, 2022
Locally the tests are passing through. And other PRs apparently has the same problem from time to time. Is possible to re-run the CI workflow? |
Codecov Report
@@ Coverage Diff @@## main #144 +/- ##
==========================================
- Coverage 86.28% 86.13% -0.16%
==========================================
Files 25 25 Lines 423 440 +17 ==========================================
+ Hits 365 379 +14 - Misses 58 61 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. |
dalssoft
commented
Jul 8, 2022
@endersoncosta about this generated code: // table.integer('userId').unsigned().nullable()// table.foreign('userId').references('id').inTable('users')(1) why about this one: knex.schema.table('markets',function(table){table.integer('companyId').unsigned().index().references('id').inTable('companys')})(5) why |
| template: `infra/data/database/${db.toLowerCase()}/migration.ejs`, | ||
| target: migrationFullPath, | ||
| props: { table: `${camelCase(name)}s`, columns: columns } | ||
| props: { table: `${camelCase(name)}s`, externalColumnName: `${camelCase(name)}Id`, columns, ref, idColumn } |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
| } | ||
| if (isArrayWithType(type) && herbs.entity.isEntity(type[0])) | ||
| refs.push({ table: `${camelCase(type[0].name)}s`, relationship: 'One-to-Many' }) |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
| if (herbs.entity.isEntity(type)) { | ||
| const typeSchema = type.prototype.meta.schema | ||
| const idRef = Object.values(typeSchema).find(column => column.options.isId)?.name | ||
| refs.push({ id: idRef, columnName: `${camelCase(name)}Id`, table: `${camelCase(type.name)}s`, relationship: 'One-to-One' }) |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
endersoncosta
commented
Jul 25, 2022
1.
|
the CLI created the default entity constUser=entity('User',{id: id(String),
...
})when adding an entity with a reference: constProject=entity('Project',{id: id(String),user: field(User),})the generated migration: .createTable('projects',function(table){table.string('id').primary()table.integer('user_id').references('id').inTable('users')here the the same is true for 1:many: .table('users',function(table){table.integer('customer_id').references('id').inTable('customers')}) |
With this we can write the relationship migrations based on our domain entities.