|
| 1 | +/** |
| 2 | + * T1756: Link field NOT NULL constraint sync bug |
| 3 | + * |
| 4 | + * Steps to reproduce: |
| 5 | + * 1. Create a Number field |
| 6 | + * 2. Set notNull=true on the Number field |
| 7 | + * 3. Convert it to a Link field |
| 8 | + * 4. Edit the Link field and turn off notNull |
| 9 | + * 5. Try to create a record with empty Link value - FAILS because DB constraint still exists |
| 10 | + */ |
| 11 | +import type { INestApplication } from '@nestjs/common'; |
| 12 | +import { FieldKeyType, FieldType, Relationship } from '@teable/core'; |
| 13 | +import type { ITableFullVo } from '@teable/openapi'; |
| 14 | +import { |
| 15 | + createField, |
| 16 | + createTable, |
| 17 | + convertField, |
| 18 | + createRecords, |
| 19 | + getField, |
| 20 | + initApp, |
| 21 | + permanentDeleteTable, |
| 22 | + deleteRecords, |
| 23 | + getRecords, |
| 24 | +} from './utils/init-app'; |
| 25 | + |
| 26 | +describe('T1756: Link field NOT NULL constraint sync bug', () => { |
| 27 | + let app: INestApplication; |
| 28 | + const baseId = globalThis.testConfig.baseId; |
| 29 | + |
| 30 | + beforeAll(async () => { |
| 31 | + const appCtx = await initApp(); |
| 32 | + app = appCtx.app; |
| 33 | + }); |
| 34 | + |
| 35 | + afterAll(async () => { |
| 36 | + await app.close(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('bug reproduction', () => { |
| 40 | + let table1: ITableFullVo; |
| 41 | + let table2: ITableFullVo; |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + table1 = await createTable(baseId, { name: `table1-${Date.now()}` }); |
| 45 | + table2 = await createTable(baseId, { name: `table2-${Date.now()}` }); |
| 46 | + |
| 47 | + // Clear default records |
| 48 | + const records1 = await getRecords(table1.id); |
| 49 | + const records2 = await getRecords(table2.id); |
| 50 | + if (records1.records.length) { |
| 51 | + await deleteRecords( |
| 52 | + table1.id, |
| 53 | + records1.records.map((r) => r.id) |
| 54 | + ); |
| 55 | + } |
| 56 | + if (records2.records.length) { |
| 57 | + await deleteRecords( |
| 58 | + table2.id, |
| 59 | + records2.records.map((r) => r.id) |
| 60 | + ); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(async () => { |
| 65 | + await permanentDeleteTable(baseId, table1.id); |
| 66 | + await permanentDeleteTable(baseId, table2.id); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should allow creating record with empty Link after removing notNull constraint', async () => { |
| 70 | + // Step 1: Create a Number field |
| 71 | + const numberField = await createField(table1.id, { |
| 72 | + name: 'TestField', |
| 73 | + type: FieldType.Number, |
| 74 | + }); |
| 75 | + |
| 76 | + // Step 2: Set notNull=true on the Number field |
| 77 | + await convertField(table1.id, numberField.id, { |
| 78 | + ...numberField, |
| 79 | + notNull: true, |
| 80 | + }); |
| 81 | + |
| 82 | + // Step 3: Convert to Link field |
| 83 | + const linkField = await convertField(table1.id, numberField.id, { |
| 84 | + type: FieldType.Link, |
| 85 | + options: { |
| 86 | + relationship: Relationship.ManyOne, |
| 87 | + foreignTableId: table2.id, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + // Step 4: Turn off notNull on the Link field |
| 92 | + const linkFieldFull = await getField(table1.id, linkField.id); |
| 93 | + const updatedLinkField = await convertField(table1.id, linkField.id, { |
| 94 | + ...linkFieldFull, |
| 95 | + notNull: false, |
| 96 | + }); |
| 97 | + |
| 98 | + // Verify metadata shows notNull is false |
| 99 | + expect(updatedLinkField.notNull).toBeFalsy(); |
| 100 | + |
| 101 | + // Step 5: Try to create a record with empty Link value |
| 102 | + // BUG: This should succeed since notNull is false in metadata |
| 103 | + // But it fails because DB still has NOT NULL constraint |
| 104 | + const result = await createRecords( |
| 105 | + table1.id, |
| 106 | + { |
| 107 | + fieldKeyType: FieldKeyType.Id, |
| 108 | + records: [{ fields: {} }], // Empty record, no Link value |
| 109 | + }, |
| 110 | + 201 // Expect success (201), but will get 500 due to DB constraint |
| 111 | + ); |
| 112 | + |
| 113 | + expect(result.records).toHaveLength(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not allow creating record with empty Link after setting notNull constraint', async () => { |
| 117 | + const linkField = await createField(table1.id, { |
| 118 | + type: FieldType.Link, |
| 119 | + options: { |
| 120 | + relationship: Relationship.ManyOne, |
| 121 | + foreignTableId: table2.id, |
| 122 | + }, |
| 123 | + }); |
| 124 | + const linkFieldFull = await getField(table1.id, linkField.id); |
| 125 | + await convertField(table1.id, linkField.id, { |
| 126 | + ...linkFieldFull, |
| 127 | + notNull: true, |
| 128 | + }); |
| 129 | + await createRecords( |
| 130 | + table1.id, |
| 131 | + { |
| 132 | + fieldKeyType: FieldKeyType.Id, |
| 133 | + records: [{ fields: {} }], // Empty record, no Link value |
| 134 | + }, |
| 135 | + 400 // Expect success (201), but will get 500 due to DB constraint |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments